gpt4 book ai didi

javascript - DOM 没有随着 Vue 中更新的内容而更新

转载 作者:行者123 更新时间:2023-12-02 21:02:45 24 4
gpt4 key购买 nike

我正在 Vue 中创建一个应用程序。我在通过事件总线显示更新内容时遇到问题。我还尝试使用 vuex 来获取本地内容,但它仍然显示相同的问题。

内容采用数组格式,具有一些对象,通过某些表单输入创建。

控制台正在显示更新的内容,但更新的内容未显示在 DOM 上。

让我添加一些屏幕截图。

enter image description here

enter image description here

谁能帮帮我吗?

让我添加一些在我的项目中使用的代码。我在代码中使用 Vue Materail。

模板 1

<md-select v-model="bedroom" name="bedroom" id="bedroom" @md-selected="getBedVal('bedroom', 10)">
<md-option value="1">1</md-option>
<md-option value="2">2</md-option>
<md-option value="3">3</md-option>
<md-option value="4">4</md-option>
<md-option value="5">5</md-option>
<md-option value="6">6</md-option>
<md-option value="7">7</md-option>
<md-option value="8">8</md-option>
<md-option value="9">9</md-option>
<md-option value="10">10</md-option>

</md-select>

组件 1:-

methods: {
ServiceInfoFun: function(obj){
var existingIds = this.getServiceInfo.map((obj) => obj.servicename);
if (!existingIds.includes(obj.servicename)) {
this.getServiceInfo.push(obj);
bus.$emit('extraservice', this.getServiceInfo);
//this.$store.dispatch('serviceListAction', this.getServiceInfo)
} else {
this.getServiceInfo.forEach((element, index) => {
if (element.servicename === obj.servicename) {
this.getServiceInfo[index] = obj;
// this.$store.dispatch('serviceListAction', this.getServiceInfo);
bus.$emit('extraservice', this.getServiceInfo);
};
});
};
},
getBedVal: function(name, price){
var obj = {
servicename: name,
serviceprice: price * this.bedroom,
serviceqty : this.bedroom
}
this.ServiceInfoFun(obj);
},
}

模板 2

{{ serviceInfo }}

组件 2:

  created(){
bus.$on('extraservice', (data) => {
this.serviceInfo = data;
console.log(this.serviceInfo);
},

提前致谢。

最佳答案

很可能您的数据是在 DOM 加载之后加载的,因为它是嵌套的,所以不会响应,因此您不能期望它会更新。

您有几个选择:

1) 在某处放置一个 v-if,以防止 DOM 在数据就位之前加载。

2)手动强制重新加载,我的首选方法是提供 key 并更改 key 。请参阅此处了解详细示例:https://michaelnthiessen.com/force-re-render/

注意:详细说明一下,如果您在 PROPS 中获取数据,那么它会出现得相当早,如果在某个生命周期中说“创建”,那么 DOM 肯定会首先加载。

关于javascript - DOM 没有随着 Vue 中更新的内容而更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61303775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com