0

当我在 Vue 3 中改变一个道具时,它是一个对象(我知道,不要直接改变道具,这里它只起作用,因为它是通过引用传递的,因为它是一个对象),它也在 Vue Dev-Tools 中更新是很棒的。

如果我发送了多个 axios 调用,并在其中解决了一些承诺,则该道具不会在 Vue 开发工具中更新,但如果我 console.log 它:我会看到所有新数据。

所以,问题是:解决异步承诺后如何更新 Vue Dev-Tools?当道具不同步时,很难调试。

export default {
  props: {
    translation: Object
  },
}

// [...]

// Vue Devtools update the prop translation
this.translation["test"] = { source: "source", target: "target" }

// I do a async call/promise with axios
Promise.all(promises)
.then((responses) => {
    responses.forEach((response) => {
        this.translation[Object.keys(response.data)[0]] =
            response.data[Object.keys(response.data)[0]];
    });
})
.then(() => {
    console.log("-------------------");
    console.log("After: Promise.all:");
    // I see that this.translation has mutated, but NOT in Vue-Devtools
    console.log(this.translation);
})
4

1 回答 1

0

您必须使用$set帮助程序:

this.$set(this.translation, Object.keys(response.data)[0], response.data[Object.keys(response.data)[0]]);

https://vuejs.org/v2/guide/reactivity.html#For-Objects

https://vuejs.org/v2/api/#Vue-set

于 2021-06-02T07:14:24.460 回答