vue 复用组件,打开两个标签页时, 数据不更新解决办法
vue官网原话:提醒一下,当使用路由参数时,例如从 /user/foo 导航到 /user/bar,原来的组件实例会被复用。因为两个路由都渲染同个组件,比起销毁再创建,复用则显得更加高效。不过,这也意味着组件的生命周期钩子不会再被调用。这就是为什么打开两个或多个详情页的时候,切换标签页数据不更新的原因。解决方法:1: watch (监测变化) $route 对象:<router-li
·
vue官网原话:提醒一下,当使用路由参数时,例如从 /user/foo 导航到 /user/bar,原来的组件实例会被复用。因为两个路由都渲染同个组件,比起销毁再创建,复用则显得更加高效。不过,这也意味着组件的生命周期钩子不会再被调用。
这就是为什么打开两个或多个详情页的时候,切换标签页数据不更新的原因。
解决方法:
1: watch (监测变化) $route 对象:
<router-link
:to="{path:'/intention_manage/intention_detail',query:{id:scope.row.inten_id}}"
>
<el-button type="text">详情</el-button>
</router-link>
watch: {
$route(to, from) {
this.id = to.query.id;
this.getData(); // 再次获取数据
}
},
methods: {
getData() {
this.$post("/intent/getintentiondetail", { inten_id: this.id }).then(
res => {
if (res.code == 200) {
this.params = res.result.intent_detail;
} else {
this.$message.error(res.msg);
}
}
);
}
}
2:可以绑定唯一的key
<router-view :key="key"></router-view>
computed: {
key() {
return this.$route.name !== undefined? this.$route.name + new Date(): this.$route + new Date()
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)