美文网首页
14种组件通讯

14种组件通讯

作者: Hello杨先生 | 来源:发表于2020-03-24 16:37 被阅读0次

    3.1 props

    这个应该非常属性,就是父传子的属性; props 值可以是一个数组或对象;

    // 数组:不建议使用
    props:[]
    
    // 对象
    props:{
     inpVal:{
      type:Number, //传入值限定类型
      // type 值可为String,Number,Boolean,Array,Object,Date,Function,Symbol
      // type 还可以是一个自定义的构造函数,并且通过 instanceof 来进行检查确认
      required: true, //是否必传
      default:200,  //默认值,对象或数组默认值必须从一个工厂函数获取如 default:()=>[]
      validator:(value) {
        // 这个值必须匹配下列字符串中的一个
        return ['success', 'warning', 'danger'].indexOf(value) !== -1
      }
     }
    }
    
    

    3.2 $emit

    这个也应该非常常见,触发子组件触发父组件给自己绑定的事件,其实就是子传父的方法

    // 父组件
    <home @title="title">
    // 子组件
    this.$emit('title',[{title:'这是title'}])
    
    

    3.3 vuex

    1.这个也是很常用的,vuex 是一个状态管理器 2.是一个独立的插件,适合数据共享多的项目里面,因为如果只是简单的通讯,使用起来会比较重 3.API

    state:定义存贮数据的仓库 ,可通过this.$store.state 或mapState访问
    getter:获取 store 值,可认为是 store 的计算属性,可通过this.$store.getter 或
           mapGetters访问
    mutation:同步改变 store 值,为什么会设计成同步,因为mutation是直接改变 store 值,
             vue 对操作进行了记录,如果是异步无法追踪改变.可通过mapMutations调用
    action:异步调用函数执行mutation,进而改变 store 值,可通过 this.$dispatch或mapActions
           访问
    modules:模块,如果状态过多,可以拆分成模块,最后在入口通过...解构引入
    复制代码
    

    3.4

    listeners

    2.4.0 新增 这两个是不常用属性,但是高级用法很常见; 1
    attrs获取子传父中未在 props 定义的值

    // 父组件
    <home title="这是标题" width="80" height="80" imgUrl="imgUrl"/>
    
    // 子组件
    mounted() {
      console.log(this.$attrs) //{title: "这是标题", width: "80", height: "80", imgUrl: "imgUrl"}
    },
    
    

    相对应的如果子组件定义了 props,打印的值就是剔除定义的属性

    props: {
      width: {
        type: String,
        default: ''
      }
    },
    mounted() {
      console.log(this.$attrs) //{title: "这是标题", height: "80", imgUrl: "imgUrl"}
    },
    
    

    listeners" 传入内部组件——在创建更高层次的组件时非常有用

    // 父组件
    <home @change="change"/>
    
    // 子组件
    mounted() {
      console.log(this.$listeners) //即可拿到 change 事件
    }
    
    

    如果是孙组件要访问父组件的属性和调用方法,直接一级一级传下去就可以

    3.inheritAttrs

    // 父组件
    <home title="这是标题" width="80" height="80" imgUrl="imgUrl"/>
    
    // 子组件
    mounted() {
      console.log(this.$attrs) //{title: "这是标题", width: "80", height: "80", imgUrl: "imgUrl"}
    },
    
    inheritAttrs默认值为true,true的意思是将父组件中除了props外的属性添加到子组件的根节点上(说明,即使设置为true,子组件仍然可以通过$attr获取到props意外的属性)
    将inheritAttrs:false后,属性就不会显示在根节点上了
    
    

    3.5 provide和inject

    2.2.0 新增 描述: provide 和 inject 主要为高阶插件/组件库提供用例。并不推荐直接用于应用程序代码中; 并且这对选项需要一起使用; 以允许一个祖先组件向其所有子孙后代注入一个依赖,不论组件层次有多深,并在起上下游关系成立的时间里始终生效。

    //父组件:
    provide: { //provide 是一个对象,提供一个属性或方法
      foo: '这是 foo',
      fooMethod:()=>{
        console.log('父组件 fooMethod 被调用')
      }
    },
    
    // 子或者孙子组件
    inject: ['foo','fooMethod'], //数组或者对象,注入到子组件
    mounted() {
      this.fooMethod()
      console.log(this.foo)
    }
    //在父组件下面所有的子组件都可以利用inject
    
    

    provide 和 inject 绑定并不是可响应的。这是官方刻意为之的。 然而,如果你传入了一个可监听的对象,那么其对象的属性还是可响应的,对象是因为是引用类型

    //父组件:
    provide: { 
      foo: '这是 foo'
    },
    mounted(){
      this.foo='这是新的 foo'
    }
    
    // 子或者孙子组件
    inject: ['foo'], 
    mounted() {
      console.log(this.foo) //子组件打印的还是'这是 foo'
    }
    
    

    3.6

    children

    children:子实例

    //父组件
    mounted(){
      console.log(this.$children) 
      //可以拿到 一级子组件的属性和方法
      //所以就可以直接改变 data,或者调用 methods 方法
    }
    
    //子组件
    mounted(){
      console.log(this.$parent) //可以拿到 parent 的属性和方法
    }
    

    parent 并不保证顺序,也不是响应式的 只能拿到一级父组件和子组件

    3.7 $refs

    // 父组件
    <home ref="home"/>
    
    mounted(){
      console.log(this.$refs.home) //即可拿到子组件的实例,就可以直接操作 data 和 methods
    }
    
    

    3.8 $root

    // 父组件
    mounted(){
      console.log(this.$root) //获取根实例,最后所有组件都是挂载到根实例上
      console.log(this.$root.$children[0]) //获取根实例的一级子组件
      console.log(this.$root.$children[0].$children[0]) //获取根实例的二级子组件
    }
    
    

    3.9 .sync

    在 vue@1.x 的时候曾作为双向绑定功能存在,即子组件可以修改父组件中的值; 在 vue@2.0 的由于违背单项数据流的设计被干掉了; 在 vue@2.3.0+ 以上版本又重新引入了这个 .sync 修饰符;

    // 父组件
    <home :title.sync="title" />
    //编译时会被扩展为
    <home :title="title"  @update:title="val => title = val"/>
    
    // 子组件
    // 所以子组件可以通过$emit 触发 update 方法改变
    mounted(){
      this.$emit("update:title", '这是新的title')
    }
    
    

    3.10 v-slot

    2.6.0 新增 1.slot,slot-cope,scope 在 2.6.0 中都被废弃,但未被移除 2.作用就是将父组件的 template 传入子组件 3.插槽分类: A.匿名插槽(也叫默认插槽): 没有命名,有且只有一个;

    // 父组件
    <todo-list> 
        <template v-slot:default>
           任意内容
           <p>我是匿名插槽 </p>
        </template>
    </todo-list> 
    
    // 子组件
    <slot>我是默认值</slot>
    //v-slot:default写上感觉和具名写法比较统一,容易理解,也可以不用写
    
    

    B.具名插槽: 相对匿名插槽组件slot标签带name命名的;

    // 父组件
    <todo-list> 
        <template v-slot:todo>
           任意内容
           <p>我是匿名插槽 </p>
        </template>
    </todo-list> 
    
    //子组件
    <slot name="todo">我是默认值</slot>
    
    

    C.作用域插槽: 子组件内数据可以被父页面拿到(解决了数据只能从父页面传递给子组件)

    // 父组件
    <todo-list>
     <template v-slot:todo="slotProps" >
       {{slotProps.user.firstName}}
     </template> 
    </todo-list> 
    //slotProps 可以随意命名
    //slotProps 接取的是子组件标签slot上属性数据的集合所有v-bind:user="user"
    
    // 子组件
    <slot name="todo" :user="user" :test="test">
        {{ user.lastName }}
     </slot> 
    data() {
        return {
          user:{
            lastName:"Zhang",
            firstName:"yue"
          },
          test:[1,2,3,4]
        }
      },
    // {{ user.lastName }}是默认数据  v-slot:todo 当父页面没有(="slotProps")
    
    

    3.11 EventBus

    1.就是声明一个全局Vue实例变量 EventBus , 把所有的通信数据,事件监听都存储到这个变量上; 2.类似于 Vuex。但这种方式只适用于极小的项目 3.原理就是利用[图片上传失败...(image-48771a-1585039061447)]

    emit 并实例化一个全局 vue 实现数据共享

    // 在 main.js
    Vue.prototype.$eventBus=new Vue()
    
    // 传值组件
    this.$eventBus.$emit('eventTarget','这是eventTarget传过来的值')
    
    // 接收组件
    this.$eventBus.$on("eventTarget",v=>{
      console.log('eventTarget',v);//这是eventTarget传过来的值
    })
    
    

    4.可以实现平级,嵌套组件传值,但是对应的事件名eventTarget必须是全局唯一的

    3.12 broadcast和dispatch

    vue 1.x 有这两个方法,事件广播和派发,但是 vue 2.x 删除了 下面是对两个方法进行的封装

    function broadcast(componentName, eventName, params) {
      this.$children.forEach(child => {
        var name = child.$options.componentName;
    
        if (name === componentName) {
          child.$emit.apply(child, [eventName].concat(params));
        } else {
          broadcast.apply(child, [componentName, eventName].concat(params));
        }
      });
    }
    export default {
      methods: {
        dispatch(componentName, eventName, params) {
          var parent = this.$parent;
          var name = parent.$options.componentName;
          while (parent && (!name || name !== componentName)) {
            parent = parent.$parent;
    
            if (parent) {
              name = parent.$options.componentName;
            }
          }
          if (parent) {
            parent.$emit.apply(parent, [eventName].concat(params));
          }
        },
        broadcast(componentName, eventName, params) {
          broadcast.call(this, componentName, eventName, params);
        }
      }
    }
    
    

    3.13 路由传参

    1.方案一

    // 路由定义
    {
      path: '/describe/:id',
      name: 'Describe',
      component: Describe
    }
    // 页面传参
    this.$router.push({
      path: `/describe/${id}`,
    })
    // 页面获取
    this.$route.params.id
    
    

    2.方案二

    // 路由定义
    {
      path: '/describe',
      name: 'Describe',
      component: Describe
    }
    // 页面传参
    this.$router.push({
      name: 'Describe',
      params: {
        id: id
      }
    })
    // 页面获取
    this.$route.params.id
    
    

    3.方案三

    // 路由定义
    {
      path: '/describe',
      name: 'Describe',
      component: Describe
    }
    // 页面传参
    this.$router.push({
      path: '/describe',
        query: {
          id: id
      `}
    )
    // 页面获取
    this.$route.query.id
    
    

    4.三种方案对比 方案二参数不会拼接在路由后面,页面刷新参数会丢失 方案一和三参数拼接在后面,丑,而且暴露了信息

    3.14 Vue.observable

    2.6.0 新增 用法:让一个对象可响应。Vue 内部会用它来处理 data 函数返回的对象; 返回的对象可以直接用于渲染函数和计算属性内,并且会在发生改变时触发相应的更新; 也可以作为最小化的跨组件状态存储器,用于简单的场景。 通讯原理实质上是利用Vue.observable实现一个简易的 vuex

    // 文件路径 - /store/store.js
    import Vue from 'vue'
    
    export const store = Vue.observable({ count: 0 })
    export const mutations = {
      setCount (count) {
        store.count = count
      }
    }
    
    //使用
    <template>
        <div>
            <label for="bookNum">数 量</label>
                <button @click="setCount(count+1)">+</button>
                <span>{{count}}</span>
                <button @click="setCount(count-1)">-</button>
        </div>
    </template>
    
    <script>
    import { store, mutations } from '../store/store' // Vue2.6新增API Observable
    
    export default {
      name: 'Add',
      computed: {
        count () {
          return store.count
        }
      },
      methods: {
        setCount: mutations.setCount
      }
    }
    </script>
    
    

    4.render 函数

    1.场景:有些代码在 template 里面写会重复很多,所以这个时候 render 函数就有作用啦

    // 根据 props 生成标签
    // 初级
    <template>
      <div>
        <div v-if="level === 1"> <slot></slot> </div>
        <p v-else-if="level === 2"> <slot></slot> </p>
        <h1 v-else-if="level === 3"> <slot></slot> </h1>
        <h2 v-else-if="level === 4"> <slot></slot> </h2>
        <strong v-else-if="level === 5"> <slot></slot> </stong>
        <textarea v-else-if="level === 6"> <slot></slot> </textarea>
      </div>
    </template>
    
    // 优化版,利用 render 函数减小了代码重复率
    <template>
      <div>
        <child :level="level">Hello world!</child>
      </div>
    </template>
    
    <script type='text/javascript'>
      import Vue from 'vue'
      Vue.component('child', {
        render(h) {
          const tag = ['div', 'p', 'strong', 'h1', 'h2', 'textarea'][this.level-1]
          return h(tag, this.$slots.default)
        },
        props: {
          level: {  type: Number,  required: true  } 
        }
      })   
      export default {
        name: 'hehe',
        data() { return { level: 3 } }
      }
    </script>
    
    
    

    2.render 和 template 的对比 前者适合复杂逻辑,后者适合逻辑简单; 后者属于声明是渲染,前者属于自定Render函数; 前者的性能较高,后者性能较低。

    相关文章

      网友评论

          本文标题:14种组件通讯

          本文链接:https://www.haomeiwen.com/subject/qwtayhtx.html