只是一个简单的问题。

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


当前回答

如果你的URL在加载组件时也发生了变化,你可以在:key属性中使用它。如果你直接在router-view标签上使用它,效果会特别好。这与添加的键的benedit是一个实际绑定到页面内容的值,而不仅仅是一些随机数。

<router-view :key="this.$route.path"></router-view>

其他回答

尝试使用这个。$router.go(0);手动重新加载当前页面。

Why?

...需要强制更新吗?

也许你没有在最好的状态下探索Vue:

要使Vue自动对值更改作出反应,必须在数据中初始声明对象。如果没有,则必须使用Vue.set()添加它们。

请参阅下面演示中的注释。或者在JSFiddle中打开相同的演示。

new Vue({ el: '#app', data: { person: { name: 'Edson' } }, methods: { changeName() { // because name is declared in data, whenever it // changes, Vue automatically updates this.person.name = 'Arantes'; }, changeNickname() { // because nickname is NOT declared in data, when it // changes, Vue will NOT automatically update this.person.nickname = 'Pele'; // although if anything else updates, this change will be seen }, changeNicknameProperly() { // when some property is NOT INITIALLY declared in data, the correct way // to add it is using Vue.set or this.$set Vue.set(this.person, 'address', '123th avenue.'); // subsequent changes can be done directly now and it will auto update this.person.address = '345th avenue.'; } } }) /* CSS just for the demo, it is not necessary at all! */ span:nth-of-type(1),button:nth-of-type(1) { color: blue; } span:nth-of-type(2),button:nth-of-type(2) { color: red; } span:nth-of-type(3),button:nth-of-type(3) { color: green; } span { font-family: monospace } <script src="https://unpkg.com/vue"></script> <div id="app"> <span>person.name: {{ person.name }}</span><br> <span>person.nickname: {{ person.nickname }}</span><br> <span>person.address: {{ person.address }}</span><br> <br> <button @click="changeName">this.person.name = 'Arantes'; (will auto update because `name` was in `data`)</button><br> <button @click="changeNickname">this.person.nickname = 'Pele'; (will NOT auto update because `nickname` was not in `data`)</button><br> <button @click="changeNicknameProperly">Vue.set(this.person, 'address', '99th st.'); (WILL auto update even though `address` was not in `data`)</button> <br> <br> For more info, read the comments in the code. Or check the docs on <b>Reactivity</b> (link below). </div>

要掌握Vue的这一部分,请查看关于反应性-更改检测警告的官方文档。这是一本必读的书!

使用v-if指令

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

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

这似乎是matthiasg关于这个问题的一个非常干净的解决方案:

你也可以使用:key=" somvariableunderyourcontrol ",当你想要完全重建组件时改变键

对于我的用例,我将Vuex getter作为道具提供给组件。Vuex会以某种方式获取数据,但反应性不会可靠地重新呈现组件。在我的例子中,将组件键设置为道具上的某个属性可以保证在getter(和属性)最终解析时进行刷新。

确定. .可以简单地使用key属性在任何时候强制重新呈现(重新创建)。

<mycomponent :key="somevalueunderyourcontrol"></mycomponent>

有关示例,请参见https://jsfiddle.net/mgoetzke/epqy1xgf/

这里也有讨论:https://github.com/vuejs/Discussion/issues/356#issuecomment-336060875