gpt4 book ai didi

vue.js - Vue.js组件在导航更改上的重用-寻求澄清

转载 作者:行者123 更新时间:2023-12-03 06:46:00 25 4
gpt4 key购买 nike

在下面的示例中,如果用户从/user/foo导航到/another_page,然后再导航到/user/bar,是否将重用User组件?

const User = {
template: '<div>User</div>'
}

const router = new VueRouter({
routes: [
{ path: '/user/:id', component: User }
]
})

Vue路由器手册说...

One thing to note when using routes with params is that when the user navigates from /user/foo to /user/bar, the same component instance will be reused. Since both routes render the same component, this is more efficient than destroying the old instance and then creating a new one. However, this also means that the lifecycle hooks of the component will not be called.



Dynamic Route Matching

...但是尚不清楚是否仅在没有中间导航步骤时才重用该组件。是否有任何文档详细讨论组件重用?

谢谢!

最佳答案

如果您从/user/foo导航到/user/bar,则该组件将被重用。
例如,在显示产品页面时经常使用。 请注意,如果需要,可以通过多种方法重建component

如果您要从/user/foo导航到/another_page,然后导航到/user/bar,则您的component在离开/another_page时将被销毁,而在导航到/user/bar时将被创建。

总结一下:
/user/foo -> /another_page-组件被破坏
/user/foo -> /user/bar-组件将被重用

关于vue.js - Vue.js组件在导航更改上的重用-寻求澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59565761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com