-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: How do I code split by route using vue-router@next? #745
Comments
Nevermind... I just didn't read through the post / docs entirely. |
@onx2 even still, I think it would be nice if you could share your findings for people coming from Google etc. and looking for an answer :) |
Sure! I simply changed it to the following: import { defineAsyncComponent } from "vue"
import { RouteRecordRaw } from "vue-router"
export const routes: Array<RouteRecordRaw> = [
{
name: "About",
path: "/about",
component: defineAsyncComponent(() => import("../views/About.vue"))
}
] [Section about generate routes removed to prevent misinformation] If someone knows how to make dynamically generated routes work, please comment. |
@onx2 does that work for you in production builds? During development it works fine, but in production it can't resolve the dynamic imports. |
I haven't tested in a production environment but I can test serving the prod build locally and let you know. Are you trying to generate routes or hard code them? |
@onx2 Vue router needs component to return a promise. So it should be instead: import { defineAsyncComponent } from "vue"
import { RouteRecordRaw } from "vue-router"
export const routes: Array<RouteRecordRaw> = [
{
...
component: () => new Promise((resolve)=> resolve(defineAsyncComponent({
loader: () => import("whatever"),
loadingComponent: Spinner,
})
)
)
}
] |
@sirdmon @MatthiasGrandl Thanks for the heads up! I haven't had to time to do any testing or to keep up with the evolving API's -- does the following work for production as well? (no options defineAsyncComponent) https://next.router.vuejs.org/guide/advanced/lazy-loading.html#lazy-loading-routes
import { defineAsyncComponent } from "vue"
import { RouteRecordRaw } from "vue-router"
export const routes: Array<RouteRecordRaw> = [
{
name: "About",
path: "/about",
component: () => new Promise(resolve => resolve(defineAsyncComponent(() => import("../views/About.vue"))))
}
] This seems pretty verbose for lazy loading / dynamic imports. Does
So I would assume A potentially helpful ref: https://v3.vuejs.org/guide/migration/async-components.html#_3-x-syntax |
None of the mentioned aproaches work with vite version:
vue-router version:
import { createWebHistory, createRouter } from "vue-router";
import {defineAsyncComponent} from "vue"
const routes = [
{
path: "/",
name: "Home",
component: () => new Promise(resolve => resolve(defineAsyncComponent(() => import("/@/views/Home.vue"))))
},
];
const router = createRouter({
history: createWebHistory(),
routes,
});
export default router; |
Please see latest glob import feature: https://vitejs.dev/guide/features.html#glob-import |
when using globEager like var routes: Array = [] const InstalledAppRoutes = import.meta.globEager('../../apps/*/routes.ts') for (const App in InstalledAppRoutes) { export default routes` in runtime it says loader not a function |
Darn, due to having multiple routes.ts files on some forgot to update the import promise as mentioned above () => import(comp). |
Hate that no one answered the original question :/ How do you set a custom name on the chunk. |
This is less or moe due to the title of the question, then when people searches for this on google this is one of the first results. |
I'd like to lazy load routes / create a chunk for each route but I can't figure out how to make it work. In Webpack I can use a dynamic import with a "magic comment" to name the chunk.
Something like this would be nice but magic comments are something I don't really care about, I'd just like the output of my build to be split by route and to have the dynamic imports reference the
.js
file.This thread seems to indicate it works now using rollup-plugin-dynamic-import-variables but using a dynamic import in the router doesn't ouput what I'd expect -- is there something else I need to configure?
The text was updated successfully, but these errors were encountered: