Pau*_*aul 4 vue.js vue-router vuejs2
我在项目中将 Vue.js 与 Vue-router 一起使用,问题是当路由更改时,包含 router-view 的组件会被销毁。这意味着页面上的所有其他组件都被销毁,因此整个事情看起来就像页面刷新一样。
我最近开始使用 Vue,我正在开发的应用程序充满了大量遗留代码,并且很难简化为基础代码,因此我很难隔离问题。
我尝试用小提琴重现该问题,但没有成功。这是模板代码:
<div id="router-app">
<router-view></router-view>
</div>
<template id="tricky-place">
<div>
<h1>Tricky place</h1>
<ul>
<li>
<router-link to="/panel1">Panel1</router-link>
</li>
<li>
<router-link to="/panel2">Panel2</router-link>
</li>
</ul>
<router-view></router-view>
</div>
</template>
<template id="tricky-place-panel1">
<div>
<h2>Panel1</h2>
</div>
</template>
<template id="tricky-place-panel2">
<div>
<h2>Panel2</h2>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
和脚本:
const TrickyPlace = Vue.component('tricky-place', {
created() {
console.log("TrickyPlace - created");
},
mounted() {
console.log("TrickyPlace - mounted");
},
updated() {
console.log("TrickyPlace - updated");
},
destroyed() {
console.log("TrickyPlace - destroyed");
},
template: '#tricky-place'
});
const TrickyPlacePanel1 = Vue.component('tricky-place-panel1', {
created() {
console.log("TrickyPlacePanel1 - created");
},
mounted() {
console.log("TrickyPlacePanel1 - mounted");
},
updated() {
console.log("TrickyPlacePanel1 - updated");
},
destroyed() {
console.log("TrickyPlacePanel1 - destroyed");
},
template: '#tricky-place-panel1'
});
const TrickyPlacePanel2 = Vue.component('tricky-place-panel2', {
created() {
console.log("TrickyPlacePanel2 - created");
},
mounted() {
console.log("TrickyPlacePanel2 - mounted");
},
updated() {
console.log("TrickyPlacePanel2 - updated");
},
destroyed() {
console.log("TrickyPlacePanel2 - destroyed");
},
template: '#tricky-place-panel2'
});
const routes = [{
path: '/',
component: TrickyPlace,
children: [{
path: 'panel1',
component: TrickyPlacePanel1
}, {
path: 'panel2',
component: TrickyPlacePanel2
}, ]
}]
const router = new VueRouter({
routes
});
const routerApp = new Vue({
router,
el: '#router-app',
});
Run Code Online (Sandbox Code Playgroud)
这是小提琴:https://jsfiddle.net/loorker/m1hncLb0/15/
这是我加载小提琴时的控制台日志,单击 Panel1,然后单击 Panel2:
TrickyPlace - created
TrickyPlace - mounted
TrickyPlacePanel1 - created
TrickyPlacePanel1 - mounted
TrickyPlace - updated
TrickyPlacePanel2 - created
TrickyPlacePanel1 - destroyed
TrickyPlacePanel2 - mounted
TrickyPlace - updated
Run Code Online (Sandbox Code Playgroud)
这正如预期的那样。
这是当我在应用程序中运行相同代码时相同操作序列的控制台日志:
TrickyPlace - created
TrickyPlace - mounted
TrickyPlace - created
TrickyPlacePanel1 - created
TrickyPlace - destroyed
TrickyPlacePanel1 - mounted
TrickyPlace - mounted
TrickyPlace - created
TrickyPlacePanel2 - created
TrickyPlacePanel1 - destroyed
TrickyPlace - destroyed
TrickyPlacePanel2 - mounted
TrickyPlace - mounted
Run Code Online (Sandbox Code Playgroud)
TrickyPlace 在每次路线更改时被销毁和创建,而不是像小提琴中那样更新。
换句话说,考虑到https://v2.vuejs.org/v2/guide/instance.html中的数字,我的应用程序中发生的情况是,在表示 Mounted 的红色圆圈中,执行流程不会采取“当数据更改”路径,但直接转到“调用 vm.$destroy() 时”。
我尝试将路由器的模式设置为“历史”,但其行为相同。应用程序中没有任何地方调用 $destroy() 。我在路由器视图上没有设置 :key 绑定。
关于如何继续查找问题的任何想法,为什么为父级调用创建/销毁,而不仅仅是更新?
谢谢。
| 归档时间: |
|
| 查看次数: |
1878 次 |
| 最近记录: |