v-for :class 更改后无效?

2019-08-04 17:21:13 +08:00
 aoscici2000

<button
    v-for="catalog in catalogs"
    :key="catalog.id"
    :class="{'active': current_catalog==catalog.name}"
    @click="change(catalog.name)"
    >
    {{ catalog.name }}
</button>

data() {
    return {
        catalogs: [{}, ...],
        current_catalog: ''
    }
}

如题: 当我更改 current_catalog 的时候, 好像 active 并没有生效?

页面并不刷新, change()只是更改了 current_catalog 的值和发一个 ajax

2613 次点击
所在节点    Vue.js
23 条回复
aoscici2000
2019-08-05 12:37:47 +08:00
@SilentDepth this.$set 也试过了,也是一样死活不响应
SilentDepth
2019-08-05 12:48:41 +08:00
刚注意到我上面说的不准确,调用 catalogs 的变异方法或直接对 catalogs 赋值是可以触发响应式更新的,只有直接访问数组元素内容时才不行。

但不管怎样 this.$set( ) 不应当失败。还是需要你发出来代码看一下。
aoscici2000
2019-08-05 14:10:06 +08:00
@SilentDepth 代码发在另外一个帖上 https://www.v2ex.com/t/589034 回复里更改了 this.$set(this, 'catalogs', resp.data.data)

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/588955

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX