admin管理员组文章数量:1315790
So I have the problem that the routes are getting loaded too late, and they exist, but when I reload the page, they won't show, so my guess is that they are getting loaded too late. I have the following code in the index.ts of the router:
fetch('http://localhost:8000/api/test/all').then((response) => response.json())
.then((data) => {
if (data) {
for (let route of data) {
router.addRoute({path: '/' + route.name, name: route.name, component: () => import('../views/TestView.vue')})
}
};
});
Is there some sort of lifecycle hook, or can I make it somehow to wait for the result before navigating to the page? I tried with beforeRouteEnter, but it did not work.
following i tried in App.vue
router.beforeEach((to, from, next) => {
fetch('http://localhost:8000/api/test/all').then((response) => response.json())
.then((data) => {
if (data) {
for (let route of data) {
router.addRoute({path: '/' + route.name, name: route.name, component: () => import('./views/TestView.vue')})
}
next()
};
});
})
but when i refresh on /xy then i get a blank site cause it didnt find /xy
So I have the problem that the routes are getting loaded too late, and they exist, but when I reload the page, they won't show, so my guess is that they are getting loaded too late. I have the following code in the index.ts of the router:
fetch('http://localhost:8000/api/test/all').then((response) => response.json())
.then((data) => {
if (data) {
for (let route of data) {
router.addRoute({path: '/' + route.name, name: route.name, component: () => import('../views/TestView.vue')})
}
};
});
Is there some sort of lifecycle hook, or can I make it somehow to wait for the result before navigating to the page? I tried with beforeRouteEnter, but it did not work.
following i tried in App.vue
router.beforeEach((to, from, next) => {
fetch('http://localhost:8000/api/test/all').then((response) => response.json())
.then((data) => {
if (data) {
for (let route of data) {
router.addRoute({path: '/' + route.name, name: route.name, component: () => import('./views/TestView.vue')})
}
next()
};
});
})
but when i refresh on /xy then i get a blank site cause it didnt find /xy
Share Improve this question edited Jan 30 at 10:42 Robin asked Jan 30 at 9:59 RobinRobin 95 bronze badges 01 Answer
Reset to default 0The first navigation happens when router plugin is enabled, this happens before the application is mounted. The most straightforward way would be:
await addDynamicRoutes();
app.use(router)
app.mount(...);
The alternative is to additionally handle this in router hook, this is covered in the documentation:
let dynamicRoutesAdded;
router.beforeEach(async (to, from) => {
if (dynamicRoutesAdded)
return;
await addDynamicRoutes();
dynamicRoutesAdded = true;
return to.fullPath;
});
next
callback is deprecated because router hooks support promises.
本文标签: vuejsHow to fetch data in router indextsStack Overflow
版权声明:本文标题:vue.js - How to fetch data in router index.ts? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741974251a2408024.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论