【发布时间】:2020-12-04 17:19:03
【问题描述】:
当我登录应用程序并选择编辑某些项目时,他们会向编辑视图创建 $router.push,问题是他们渲染组件两次,我通过在挂载上执行 console.log 来解决这个问题(){}.. 但是,如果我重新加载页面并在其他时间单击编辑,它们会正确呈现,只有一次。
这是相关代码:
//listItemsView script
editItem(item) {
this.$router.push({ name: 'editPolicy', params:{policyTest: item}})
},
//editItemView script
export default {
props:{
policyTest:{
type: Object,
required: true,
}
mounted(){
console.log(this.policyTest);
console.log('entra');
},
}
//router script
{
path: '/editPolicy/',
name: 'editPolicy',
component: () => import('../views/policies/editPolicy.vue'),
props: true,
meta:{requireAuth:true}
}
router.beforeEach((to, from, next) => {
const user = auth.currentUser;
if(user !== null){
user.getIdTokenResult(true)
.then(function ({
claims
}) {
if (to.name === 'NewClient' && !claims.permissions.includes('Agregar Cliente')) {
next({name: 'notFoundPage'});
}else{
//In this case the router execute this next()
next()
}
})
} else {
if (to.matched.some(record => record.meta.requireAuth)) {
next({name: 'SignIn'});
} else {
next()
}
}
})
//html
<td class="text-left">
<v-icon small class="mr-2" @click="editItem(item)">fas fa-edit</v-icon>
</td>
【问题讨论】:
-
看起来 Vue 在更改路由时正在尝试重用组件。您可以尝试使用
<router-view :key="$route.fullPath">,如下所述:stackoverflow.com/a/52848095/10387396 -
它安装了两次,我的猜测是有两个不同的实例,也许检查跟踪。但您也可以键入这些组件以确保它们被重用。只需在 mount 或 log.error 中编写调试器新错误。了解实际发生的情况。
标签: vue.js vue-router