只是一个简单的问题。
你能强迫Vue.js重新加载/重新计算所有内容吗?如果有,怎么做?
只是一个简单的问题。
你能强迫Vue.js重新加载/重新计算所有内容吗?如果有,怎么做?
当前回答
添加:key到vue-router库的router-view组件的方法会引起我的fickers,所以我使用vue-router的“组件内保护”来拦截更新,并在同一路由上的路径更新时相应地刷新整个页面(如$router. exe)。美元,路由器。推,路由器。替换也无济于事)。唯一需要注意的是,通过刷新页面,我们暂时打破了单页应用行为。
beforeRouteUpdate(to, from, next) {
if (to.path !== from.path) {
window.location = to.path;
}
},
其他回答
使用vm。美元(varName,价值)。
在Change_Detection_Caveats中查看详细信息
确定. .可以简单地使用key属性在任何时候强制重新呈现(重新创建)。
<mycomponent :key="somevalueunderyourcontrol"></mycomponent>
有关示例,请参见https://jsfiddle.net/mgoetzke/epqy1xgf/
这里也有讨论:https://github.com/vuejs/Discussion/issues/356#issuecomment-336060875
我找到了一个办法。这有点俗气,但很管用。
vm.$set("x",0);
vm.$delete("x");
其中vm是视图模型对象,x是一个不存在的变量。
Vue.js会在控制台日志中抱怨这个问题,但它确实会触发所有数据的刷新。使用版本1.0.26进行测试。
<router-view :key="$route.params.slug" />
只需使用你的任何参数的关键,它的自动重载子..
请读一读 http://michaelnthiessen.com/force-re-render/
可怕的方式:重新加载整个页面 更好的方法:使用Vue的内置 最好的方法:在你的 组件
<template>
<component-to-re-render :key="componentKey" />
</template>
<script>
export default {
data() {
return {
componentKey: 0,
};
},
methods: {
forceRerender() {
this.componentKey += 1;
}
}
}
</script>
在某些情况下,我也用watch。