I'm using vue-router and have problems with registering components when dynamically importing.
If I use the basic hard coded code, the router will work fine. But if I load my routes in dynamically it fails to render my component.
dynamically importing the component file:
GetAllPages((data) => {
let pages = [];
data.map(page => {
const pageTitle = page.title.rendered.toLowerCase();
import(`../pages/${pageTitle}.js`).then(module => {
pages.push({
path: `/${pageTitle}`,
name: `${pageTitle}`,
component: module.default
})
})
})
return pages;
})
router function:
new VueRouter({
mode: 'history',
// routes: pages <= not registering data
routes: [
{path: '/about', component: About} // <= working fine
]
});
dynamically imported file:
let AboutPage = {
data: () => ({
Message: "hey"
}),
render: () => {
return "<h1>{{Message}}</h1>";
}
}
export default Vue.component("About", {
data: AboutPage.data,
template: AboutPage.render()
})
The problem is that the hard coded routes will work, and the dynamically imported pages do not. even if I hard code the dynamically imported values in the data.map function it will not render out my page. There are no errors from the router in the console.
PS: if I console.log the router instance, the dynamiccally imported routes are set. but not rendering html.
return pages;
it'll be an empty array, that will get populated in the future - perhaps that's the issue - how do you useGetAllPages
function anyway? You haven't shown that ā Bravopages
will be fully populated when returned, then you don't understand asynchrony ā Bravo