只是一个简单的问题。

你能强迫Vue.js重新加载/重新计算所有内容吗?如果有,怎么做?


当前回答

这对我来说很有效。

created() {
    EventBus.$on('refresh-stores-list', () => {
        this.$forceUpdate();
    });
},

另一个组件触发refresh-stores-list事件将导致当前组件重新呈现

其他回答

试试这个神奇的咒语:

vm.$forceUpdate();
//or in file components
this.$forceUpdate();

不需要创建任何悬挂变量:)

更新:当我刚开始使用VueJS时,我发现了这个解决方案。然而,进一步的探索证明这种方法是一种拐杖。据我回忆,在一段时间内,我摆脱了它,只是把所有不能自动刷新的属性(大部分是嵌套的)放入计算属性中。

更多信息请访问:https://v2.vuejs.org/v2/guide/computed.html

除了页面重载方法(闪烁),没有一个适合我(:键没有工作)。

我发现这个方法从旧vue.js论坛,这是为我工作:

https://github.com/vuejs/Discussion/issues/356

<template>
    <div v-if="show">
       <button @click="rerender">re-render</button>
    </div>
</template>
<script>
    export default {
        data(){
            return {show:true}
        },
        methods:{
            rerender(){
                this.show = false
                this.$nextTick(() => {
                    this.show = true
                    console.log('re-render start')
                    this.$nextTick(() => {
                        console.log('re-render end')
                    })
                })
            }
        }
    }
</script>

为了重新加载/重新渲染/刷新组件,停止长编码。Vue.JS可以做到这一点。

只需使用:key属性。

例如:

<my-component :key="unique" />

我正在使用BS Vue表槽。我要为这个组件做一些事情,让它是唯一的。

使用v-if指令

<div v-if="trulyvalue">
  <component-here />
</div>

因此,简单地将truyvalue的值从false更改为true将导致div之间的组件重新渲染

请读一读 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。