Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
842 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 Answer

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...