解决Vue数据更新数据不渲染问题

  • Vue包装了数个数组操作函数,使用这些方法操作的数组去,其数据变动时会被vue监测: 
    • push()
    • pop()
    • shift()
    • unshift()
    • splice()
    • sort()
    • reverse()
    • vue2.0还增加个方法可以观测Vue.set(items, indexOfItem, newValue)
    • filter(), concat(), slice() 。这些不会改变原始数组,但总是返回一个新数组。当使用非变异方法时,可以用新数组替换旧数组
  • Vue 不能检测以下变动的数组: 
    • ① 当你利用索引直接设置一个项时,vm.items[indexOfItem] = newValue
    • ② 当你修改数组的长度时,例如: vm.items.length = newLength
function getFoolTypeAllData(){
	$.ajax({
		url:projectName+"/fool/getFoolType",
		type:"get",
		success:function(data){
			$.each(data,function(a,b){
				  // this.foolType[a] ={id:b.id,type:b.type} //这样直接修改不能被vue监听到
			          // Vue.set(this.foolType,a,{id:b.id,type:b.type}); //这样就能被vue监控到,更新视图  
				 Vue.set(this.foolType,a,{id:b.id,type:b.type});
			})
		},error:function(){
			layer.msg("服务器异常!获取菜品类型失败", {icon: 5});
		}
	})
	
}

通过后台接口获取数据,封装到Vue的定义的变量foolType[]中,页面通过v-for循环渲染出数据,再也不会出现刷新几次没有数据的问题了

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你在 Vue 中使用表格组件并且更新数据但没有渲染出来,可能是因为你没有使用正确的方式来更新数据。 一种解决方法是使用 Vue 的响应式数据,确保每次更新数据时都能够自动重新渲染。 另一种解决方法是使用 Vue 的强制更新功能,可以手动触发组件的重新渲染。 以下是使用响应式数据更新表格的示例代码: ```html <template> <div> <table> <thead> <tr> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr v-for="(person, index) in people" :key="index"> <td>{{ person.name }}</td> <td>{{ person.age }}</td> </tr> </tbody> </table> <button @click="updateData">Update Data</button> </div> </template> <script> export default { data() { return { people: [ { name: "John", age: 30 }, { name: "Jane", age: 25 }, { name: "Bob", age: 40 }, ], }; }, methods: { updateData() { // 修改数据并触发重新渲染 this.people[0].age = 31; this.people[1].age = 26; this.people[2].age = 41; }, }, }; </script> ``` 如果你使用了类似 `this.people[0].age = 31` 这样直接修改数据的方式,那么需要使用 `this.$forceUpdate()` 手动触发重新渲染。 ```html <template> <div> <table> <thead> <tr> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr v-for="(person, index) in people" :key="index"> <td>{{ person.name }}</td> <td>{{ person.age }}</td> </tr> </tbody> </table> <button @click="updateData">Update Data</button> </div> </template> <script> export default { data() { return { people: [ { name: "John", age: 30 }, { name: "Jane", age: 25 }, { name: "Bob", age: 40 }, ], }; }, methods: { updateData() { // 修改数据 this.people[0].age = 31; this.people[1].age = 26; this.people[2].age = 41; // 手动触发重新渲染 this.$forceUpdate(); }, }, }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值