Vue.js - update router view

纵饮孤独 提交于 2020-03-12 05:26:03

问题


I am new to Vue.js and encountered this problem.

I have this simple piece of code in App.vue

<div v-for="brand in response" v-bind:key="brand.BrandId">
    <router-link v-bind:to="{name: 'brand', params: {brandId: brand.BrandId } }">
        {{brand.Name}}
    </router-link>
</div>
<router-view />

The router/index.js routes array item looks like this:

{
    path: '/brand/:brandId',
    name: 'brand',
    component: () => import('../views/BrandDetail.vue')
}

I received the response from API. It is a valid array of objects. The menu is showing fine.

I would expect the router view to update on the click of the router-link. It does update the URL (#/brand/id), but the router view does not update.

There are other router-links that are hardcoded. If I go there and back to any dynamically added router-link it works as expected but if I click one dynamic router-link and then another the router-view is stuck in the first one.

I also tried to add a reactive data source to the key but that did not help.

Can someone explain to me what is going on here?


回答1:


This happens when you enter a route you are already on, and the component is not reloaded, even though the parameters are different. Change your router-view to:

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

Vue tries to reuse components when possible, which is not what you want in this situation. The key attribute tells Vue to use a different instance of a component for each unique key rather than reusing one. Since the path is different for each set of parameters, that will make a good key.




回答2:


You need to include props: true

{
    path: '/brand/:brandId',
    props: true,
    name: 'brand',
    component: () => import('../views/BrandDetail.vue')
}


来源:https://stackoverflow.com/questions/60057473/vue-js-update-router-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!