zoukankan      html  css  js  c++  java
  • Vue父子组件之间通信

    1、父 -> 子。通过props

    //father.vue
    <template>
        <div id="father">
            <div><label>姓名</label><input type="text" v-model="name"/></div>
        </div>
    </template>
    
    <script>
        import child from './child'
        export default {
            data() {
                return {
                    name: ''
                }
            },
            components: { child }
        }
    </script>
    
    <style scoped>
    
    </style>
    //child.vue
    <template>
        <div id="child">
            <div>父组件传过来的值:{{msg}}</div>
        </div>
    </template>
    
    <script>
        export default {
            props: ['msg']
        }
    </script>
    
    <style scoped>
    
    </style>

    2、子 -> 父 通过emit事件触发父组件上的方法

    //father.vue
    <template>
        <div id="father">
            <div><label>姓名</label><input type="text" v-model="name"/></div>
            <div style="margin-top:20px">
                <child :msg="name" @msgFt="fun"></child>
            </div>
        </div>
    </template>
    
    <script>
        import child from './child'
        export default {
            data() {
                return {
                    name: ''
                }
            },
            methods: {
                fun(val) {
                    console.log(val)
                    this.name = val
                }
            },
            components: { child }
        }
    </script>
    
    <style scoped>
    
    </style>
    //child.vue
    <template>
        <div id="child">
            <div>父组件传过来的值:{{msg}}</div>
            <input type="text" v-model="name" />
            <button type="button" @click="handle">传给父组件</button>
        </div>
    </template>
    
    <script>
        export default {
            props: ['msg'],
            data() {
                return {
                    name: ''
                }
            },
            methods: {
                handle() {
                    this.$emit('msgFt',this.name)
                }
            }
        }
    </script>
    
    <style scoped>
    
    </style>
  • 相关阅读:
    P1604&P1601
    【USACO OPEN 10】hop
    [usaco2008feb_gold]路面修整
    bzoj1016: [JSOI2008]最小生成树计数
    bzoj1015: [JSOI2008]星球大战starwar
    bzoj1014: [JSOI2008]火星人prefix
    [bzoj3223]文艺平衡树
    bzoj3224: Tyvj 1728 普通平衡树
    bzoj1012: [JSOI2008]最大数maxnumber
    P3369 【模板】普通平衡树 (splay 模板)
  • 原文地址:https://www.cnblogs.com/zengfp/p/9621485.html
Copyright © 2011-2022 走看看