只是一个简单的问题。
你能强迫Vue.js重新加载/重新计算所有内容吗?如果有,怎么做?
只是一个简单的问题。
你能强迫Vue.js重新加载/重新计算所有内容吗?如果有,怎么做?
当前回答
为我工作
data () {
return {
userInfo: null,
offers: null
}
},
watch: {
'$route'() {
this.userInfo = null
this.offers = null
this.loadUserInfo()
this.getUserOffers()
}
}
其他回答
<my-component :key="uniqueKey" />
同时使用。$set(obj,'obj_key',value) 并且每次更新object (obj)值时更新uniqueKey 对于每一次更新this. uniquekey++
这对我来说很管用
添加:key到vue-router库的router-view组件的方法会引起我的fickers,所以我使用vue-router的“组件内保护”来拦截更新,并在同一路由上的路径更新时相应地刷新整个页面(如$router. exe)。美元,路由器。推,路由器。替换也无济于事)。唯一需要注意的是,通过刷新页面,我们暂时打破了单页应用行为。
beforeRouteUpdate(to, from, next) {
if (to.path !== from.path) {
window.location = to.path;
}
},
试试这个神奇的咒语:
vm.$forceUpdate();
//or in file components
this.$forceUpdate();
不需要创建任何悬挂变量:)
更新:当我刚开始使用VueJS时,我发现了这个解决方案。然而,进一步的探索证明这种方法是一种拐杖。据我回忆,在一段时间内,我摆脱了它,只是把所有不能自动刷新的属性(大部分是嵌套的)放入计算属性中。
更多信息请访问:https://v2.vuejs.org/v2/guide/computed.html
添加以下代码:
this.$forceUpdate()
有两种方法,
你可以在你的方法处理程序中使用$forceUpdate()
<您的组件@click = " reRender ()" > < /组件>
<script>
export default {
methods: {
reRender(){
this.$forceUpdate()
}
}
}
</script>
你可以给你的组件一个:key属性,并在想要渲染时增加
<your-component:key="index" @click="reRender()" > < /组件>
<script>
export default {
data() {
return {
index: 1
}
},
methods: {
reRender(){
this.index++
}
}
}
</script>