简体   繁体   English

页面加载时的 vue-router 动态路由

[英]vue-router dynamic routes on pageload

Using vue-router 3.0.6使用vue-router 3.0.6

I have several landing pages, whose URLs are loaded on page load via an API.我有几个登陆页面,它们的 URL 是通过 API 在页面加载时加载的。

I wait to render <router-view /> until this API call has resolved.我等待渲染<router-view />直到这个 API 调用得到解决。

When it has resolved I run this.$router.addRoutes(routes)解决后,我运行this.$router.addRoutes(routes)

However once router-view is render, I get a 404 route rendered.但是,一旦渲染了路由器视图,我就会得到一个 404 路由。

If I navigate to the homepage and click a link to the dynamic landing page I was originally trying to access on load it works fine .如果我导航到主页并单击指向我最初尝试在加载时访问的动态登录页面的链接,它工作正常

Is there some function to get vue-router to re-evaluate it's available routes, something that might be happening during the route change to the homepage that allows the next navigation to the dynamic page work?是否有一些 function 让 vue-router 重新评估它的可用路由,在路由更改到主页期间可能发生的事情允许下一次导航到动态页面工作?

First load -> /dynamic-route = 404第一次加载 -> /dynamic-route = 404

First load -> /dynamic-route -> 404 -> homepage -> /dynamic-route = OK首先加载 -> /dynamic-route -> 404 -> 主页 -> /dynamic-route = OK

when you match /dynamic-route directely, maybe the addRoutes did not work. 直接匹配/dynamic-route时,可能addRoutes不起作用。 so it would math 404. you can make the function of addRoutes work using the function of beforeEach when you change your route. 如此一来数学404。当您更改路线时,可以使用beforeEach函数使beforeEach功能beforeEach

Old question but if anyone is stuck on this, you need to perform the operation that sets the routes before calling Vue.use(router) in Vue 2 or app.use(router) in Vue 3.老问题,但如果有人卡在这个问题上,您需要在 Vue 2 中调用 Vue.use(router) 或在 Vue 3 中调用 app.use(router) 之前执行设置路由的操作。

This is because installing the router plugin immediately triggers an operation to match the initial path.这是因为安装路由器插件会立即触发匹配初始路径的操作。 Since the route you wish it to detect is not registered at that time, it gets missed.由于您希望它检测的路线当时没有注册,因此它会被错过。

It does not matter if you show/hide the router-view component based on a flag variable since the path matching operation has already been completed by then.是否基于标志变量显示/隐藏路由器视图组件无关紧要,因为此时路径匹配操作已经完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM