• vue之组件之间的传值


    1、父组件向子组件传值
    //parent.vue
    <template>
    <div>
    <child :send-msg-to-child="toChildMsg"></child>
    </div>
    </template>
    <script>
    import child from './child.vue';
    export default{
    name:'parent',
    props:[],
    data(){
    return {
    toChildMsg:'about data to child component'
    }
    },
    components:{
    child
    }
    }
    </script>
    ---------------------------------------------------
    //child.vue
    <template>
    <div>{{sendMsgToChild}}</div>
    </template>
    <script>
    export default{
    name:'child',
    props:['sendMsgToChild'],
    data(){
    return {

    }
    },
    methods:{

    }
    }
    </script>
    2、子组件向父组件传值
    child.vue

    <template>
    <div @click="up">向父组件传递数据</div>
    </template>
    <script>
    import child from './child.vue';
    export default{
    name:'child',
    props:[''],
    data(){
    return {

    }
    },
    methods:{
    up(){
    this.$emit('up-up','this is data to parent component')//主动触发upup事件,然后向父组件传递数据
    }
    }
    }

    </script>

    ---------------------------------------------------

    parent.vue

    <template>
    <div>
    <child @up-up="getDataFromChild"></child>/*监听子组件触发的upup事件,然后调用getDataFromChild方法获取数据*/
    <div>{{getChildMsg}}</div>
    </div>
    </template>
    <script>
    import child from './child.vue';
    export default{
    name:'parent',
    props:[],
    data(){
    return {
    getChildMsg:null
    }
    },
    components:{
    child
    },
    methods:{
    getDataFromChild(msg){
    this.getChildMsg = msg;
    }
    }
    }
    </script>
    3、非父子组件之间的传值
    bus.js

    import Vue from 'vue'
    const bus = new Vue();
    export default bus
    ---------------------------------------------------
    component1.vue

    <template>
    <div>
    <div @click="toCompontent2">非父子组件传递</div>
    </div>
    </template>
    <script>
    import bus from '../javascript/bus.js'
    export default{
    name:'component1',
    props:[],
    data(){
    return {

    }
    },
    components:{

    },
    methods:{
    toCompontent2(){
    bus.$emit('change','data to another component')
    }
    }
    }

    </script>

    ---------------------------------------------------

    component2.vue

    <template>
    <div>
    {{getDataFromComponent}}
    </div>
    </template>
    <script>
    import bus from '../javascript/bus.js'
    export default{
    name:'component2',
    props:[],
    data(){
    return {
    getDataFromComponent:null
    }
    },
    components:{

    },
    mounted:{
    bus.$on('change',msg=>{
    this.getDataFromComponent = msg;
    })
    }
    }

    </script>
     


  • 相关阅读:
    斐波那契数列 的两种实现方式(Java)
    单链表反转
    单链表合并
    两个有序list合并
    list去重 转载
    RemoveAll 要重写equals方法
    Java for LeetCode 138 Copy List with Random Pointer
    Java for LeetCode 137 Single Number II
    Java for LeetCode 136 Single Number
    Java for LeetCode 135 Candy
  • 原文地址:https://www.cnblogs.com/zlq92/p/10123443.html
Copyright © 2020-2023  润新知