标签切换不生效

来源:3-4 实现todo应用的业务逻辑

慕慕6102385

2019-04-02 10:45

为何tabs.vue内的@click事件都生效呢?

<span

        v-for="state in states"

        :key="state"

          :class="[state, filter === state ? 'actived' : '']"

        @click="toggleFilter(state)"

>

        {{state}}

</span>


写回答 关注

1回答

  • 鼬先生
    2019-08-16 17:20:41

    tabs中的筛选状态取决于todo里面传过来的filter.你要通过事件总线($emit)将当前的点击的状态发送给父组件todo.vue,再在todo里面通过实践监听来将传过来的state定义为传过去的filter,以改变筛选标签.

    tabs.vue:

    methods: {

        clearAllCompleted() {

            this.$emit('clearAll');

        },

        toggleFilter(state) {

            this.$emit('toggle', state);

        }

    }

    todo.vue:

    HTML部分:

    <Tabs

         :filter="filter"

         :todos="todos"

         @toggle="toggleFilter"

         @clearAll="clearAllCompletedTodo"

       />

    script部分:

    methods: {

        addTodo(e) {

            this.todos.unshift({

                id: id++,

                content: e.target.value.trim(),

                completed: false

             });

            e.target.value = '';

         },

        deleteTodo(id) {

            this.todos.splice(this.todos.findIndex(todo => id === todo.id), 1);

        },

        toggleFilter(state) {

            this.filter = state;

        },

        clearAllCompletedTodo() {

            this.todos = this.todos.filter(todo=> todo.completed===false);

        }

    }


Vue+Webpack打造todo应用

用前端最热门框架Vue+最火打包工具Webpack打造todo应用

84606 学习 · 787 问题

查看课程

相似问题