vue数据强制更新的办法
方法1:用forceUpdate直接在需要更新得地方加入‘forceUpdate直接在需要更新得地方加入`forceUpdate直接在需要更新得地方加入‘forceUpdate`如:methods:{updateName(){this.userInfo.name='小红'//在此时,确实已经将userInfo对象修改完成console.log(this.userInfo.name);//输出结果:
·
方法1:用$forceUpdate
直接在需要更新得地方加入$forceUpdate
如:
methods:{
updateName(){
this.userInfo.name='小红'//在此时,确实已经将userInfo对象修改完成
console.log(this.userInfo.name);//输出结果: 小红
this.$forceUpdate();//在这里,强制刷新之后,页面的结果变为'小红'
}
}
方法2:用$set
this.$set(this.userInfo,'name','小红') //第一个参数传要更新得父级 第二个参数为字符串得子级,第三个参数为具体数值
个人比较推荐用$set
方法,$forceUpdate
方法对资源消耗比较大
更多推荐
已为社区贡献1条内容
所有评论(0)