美文网首页
vue组件传值详解

vue组件传值详解

作者: LazyCat404 | 来源:发表于2019-07-11 10:50 被阅读0次

全局组件VS局部组件

  1. 全局组件

全局可调用,可在一个或多个挂在了Vue实例的DOM节点内使用该组件。

<body>
    <div id="app1">
        <my-component></my-component>
    </div>
    <div id="app2">
        <my-component></my-component>
    </div>
    <!-- js -->
    <script>
        Vue.component("my-component",{
            template:`<div>我是全局组件</div>`
        })
        new Vue({
            el:'#app1',
        })
        new Vue({
            el:'#app2'
        })
    </script>
</body>
  1. 局部组件

在Vue实例内声明的组件,只能在该实例内调用,局部组件和全局组件同时存在(组件名相同)时,局部组件会覆盖全局组件。

<body>
    <div id="app">
        <my-component></my-component>
    </div>
    <!-- js -->
    <script>
        new Vue({
            el:'#app',
            components:{
                'my-component':{
                    template:`<div>我是局部组件</div>`,
                }
            }
        })
    </script>
</body>

组件间通信方案

  1. 通过props传递

  2. $emit触发自定义事件

  3. 使用 ref

  4. 总线,EventBus

  5. parent/root

  6. attrslisteners

  7. ProvideInject

  8. Vuex

父->子

父组件通过属性赋值的方式向子组件传值,子组件通过props接收,使用。

<body>
    <div id="app">
        <my-component :title="title"></my-component>
    </div>
    <!-- js -->
    <script>
        Vue.component("my-component",{
            template:`<div @click='cont++'>{{cont}}</div>`,
            data(){ //子组件的data必须是函数
                return{
                    cont:this.title, //将接收的值暂存
                }
            },
            props:{
                title:{
                    type: Number, //传过来的值必须是Number类型
                    default:0,
                    required:true //传值不能为空
                }
            }
        })
        new Vue({
            el:'#app', 
            data(){
                return{
                    title:0 //父组件绑定传值
                }
            }, 
        })
    </script>
</body>

子->父

子组件通过向外触发方法的方式向父组件传值。

<body>
    <div id="app">
        {{total}}
        <my-component @addtotal="add"></my-component>
        <my-component v-on:addtotal="add"></my-component>
    </div>
    <!-- js -->
    <script>
        Vue.component("my-component",{
            template:`<div @click='addCont'>{{cont}}</div>`,
            data(){
                return{
                    cont:0 
                }
            },
            methods:{
                addCont(){
                    this.cont++;
                    this.$emit("addtotal");//子组件通过$emit向上触发方法
                }
            },
        })
        new Vue({
            el:'#app', 
            data(){
                return{
                    total:0, 
                }
            }, 
            methods:{
                add(){
                    this.total++;
                }
            }
        })
    </script>
</body>

ref

父组件获取子组件实例,调用子组件方法、获取子组件数据

// 子组件
<Children ref='son'></Children>

this.$refs.son  // 子组件实例,可继续 . 拿到对应数据

EventBus

适用于:兄弟组件、无关系组件

// 创建总线(vue 已经实现了Bus)
class Bus {
    constructor(){
        this.callbacks = {};    // 存放事件的名字
    }
    $on(name,fn){
        this.callbacks[name] = this.callbacks[name] || [];
        this.callbacks[name].push(fn);
    }
    $emit(name,args){
        if(this.callbacks[name]){
            this.callbacks[name].forEach(cb => cb(args));
        }
    }
}

// main.js (挂在在vue实例的原形上)
Vue.prototype.$bus = new Bus();

// 未完……

相关文章

  • Vue - 传值

    Vue 传值有两种:1)父组件向子组件传值(属性传值,Prop传值)2)子组件向父组件传值 一、父组件向子组件传值...

  • (VUE3) 四、组件传值(父子组件传值 & 祖孙组件传值 &v

    1.父子组件传值 vue2中的父子组件传值:父组件: 子组件: vue3中的父子组件传值: 还是用props接收父...

  • VUE组件(传值,生命周期)

    VUE生命周期 VUE子传父组件通信 VUE非父子组件传值

  • 前端基础搬运工-VUE模块

    十、VUE模块 基础部分 1. Vue组件间传值 答: -[ ] 1.父子之间的传值 父组件向子组件传值通过p...

  • Vue父子组件通信和双向绑定

    本篇文章主要介绍父子组件传值,组件的数据双向绑定。 1. 基础父子组件传值 父子组件传值,这是Vue组件传值最常见...

  • vue组件传值详解

    全局组件VS局部组件 1.全局组件全局可调用,可在一个或多个挂在了Vue实例的DOM节点内使用该组件。 2.局部组...

  • 2019-03-13

    vue父子组件传值,(父组件向子组件传值用prop ,子组件向父组件传值:子组件调用父组件方法值以参数的方式传递)...

  • 2019-03-13

    vue父子组件传值,(父组件向子组件传值用prop ,子组件向父组件传值:子组件调用父组件方法值以参数的方式传递)...

  • vue2.0的三种常用传值方式,并且如何实现?

    vue2.0 组件传值方式有三种:父组件向子组件传值,子组件向父组件传值,非父子组件传值 : 父传子: 首先现在父...

  • Vue组件传值

    vue组件传值 一、父组件向子组件传值方式: 1. 子组件中定义props,父组件向子组件props进行传值。 2...

网友评论

      本文标题:vue组件传值详解

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