Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
859 views
in Technique[技术] by (71.8m points)

vue 双向绑定问题$emit无效

父组件变量传给子组件,子组件修改后回传给父组件,无法修改

<div id="app">
    <navbar :list="nav" :current="current"></navbar>
</div>

<template id="navbar">
    <div class="col" v-for="(item,index) in list" @click="setCurrent(index)">
        <div :class="{current:current==index}">{{item.name}}</div>
    </div>
</template>

<script>
    new Vue({
        el: '#app',
        data: {
            nav: [{
                name: '标题',
                type: 'title'
            }, {
                name: '颜色',
                type: 'color'
            }],
            current: 0
        },
        methods: {
            onCurrent: function(index) {
                console.log(index);
                this.current = index;
            }
        },
        components: {
            navbar: {
                props: ['list', 'current'],
                template: '#navbar',
                methods: {
                    setCurrent: function(index) {
                        this.$emit('on-current', index);
                    }
                }
            }
        }
    });
    </script>

子组件中setCurrent正常执行,父组件onCurrent却没有反应,而且父组件的current变量未发生变化;

有哪位大大帮看看什么情况


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

在你代码中没有看到你使用到onCurrent这个方法

<div id="app">
    <navbar :list="nav" :current="current" @on-current="onCurrent"></navbar>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...