-
-
Notifications
You must be signed in to change notification settings - Fork 5.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
Loading asynchronously a non .vue component fails #1379
Comments
This is because you define an es6 export, but use commonjs require. In that case, you have to actually extract the component from the It should work as expected if you use es6 import style from webpack: component: () => import('. /async.js') Vue-loqder works around that problem, but because of this, vue files cannot have named exports. So this is a trade-off. |
Thanks LinusBorg for your answer, this syntax does not compile in JS or TS with my setup. I granted you some access in this repo https://github.com/fpoliquin/vue-bug-async.git if you could show me a working example. Thanks. |
component: () => import('. /async.js').then(m => m.default) |
Hi @yyx990803, thank you for your answer. Typescript doesn't support this synthax yet so I had to change it a little bit like this : component: = (resolve) => (require as any)(['./async'], function (module) {
resolve(module.default)
}) And now it is working. I don't know if it would be a good idea do update the documentation with this. I know I lost a lot of time figuring this out. Thanks for your help. |
Amazing @fpoliquin 💃💃if someone has problems I was exporting my modules without default: app.router.ts
./model-explorer/model-explorer.component
And if you'te using webpack don't forget
(your path could be other) |
set component: () => import('. /async.js').then(m => m) |
Hey guys, I cannot get my configuration to work. I have the same problem as fpoliquin. If I want to lazy load my component I get this message: Failed to mount component: template or render function not defined. If I use the normal way the routing is working. { path: '/forum', component: Forum} // is working
{ path: '/forum', component: () => import('./components/Forum.vue') } //is not working I am almost sure that it has to do with my webpack configuration. I am using the config for quasar 0.14 so I can see changes on my phone as well which is very handy. If anybody could help me or point me in a direction I would be very grateful. I uploaded my project on github: https://github.com/JDrechsler/Problem-with-vue-router Thank you Johannes |
What versions of vue-loader and vue-router are you using? vue-loader 13.0 introduced changes that required an upgrade of vue-router for this to work. With older versions of the router, you now have to do { path: '/forum', component: () => import('./components/Forum.vue').then(m => m.default) } / |
Thank you very much! This actually did the trick and it is now working again. I am using a version by Daniel Rosenwasser so I can use TypeScript in a nicer way. "vue": "git+https://github.com/DanielRosenwasser/vue.git#540a38fb21adb7a7bc394c65e23e6cffb36cd867", |
Now I can auto register my routes again. Thank you! function load(compName: string) {
return () => import(`./components/${compName}.vue`).then(m => m.default)
}
declare var compNamesFromWebpack: string[]
var routes: { path: string, component: () => Promise<any> }[] = []
compNamesFromWebpack.forEach(element => {
var compName = element.replace('.vue', '')
var compDomainName = `/${compName.toLocaleLowerCase()}`
console.log(`Created route ${compDomainName}`)
routes.push({ path: compDomainName, component: load(compName) })
});
routes.push({ path: '/', component: load('Home') })
routes.push({ path: '*', component: load('Error404') }) |
Finally I've the best solution, simply reading the official docs and your answers https://router.vuejs.org/en/advanced/lazy-loading.html 1.- tsconfig.json
2.- .babelrc
3.- webpack.config.js
4.- Configure routes
Now is perfect :D 💃 |
@CKGrafico shoud it work without babel? My
and main
And I get following error on compile:
looks like typescript cannot resolve type of dynamically imported component. |
I think that is not possible without babel now |
@CKGrafico Thank you for a quick reply. I confirm that your solution works fine with babel |
@cybernetlab @CKGrafico This is my setup:
and
this is the error I'm getting...
Do I also need to add babel somewhere in my webpack.config.js? And am I supposed to create a .bablerc file? If so, how do you implement this file?? Or is it simply the way I have my components structured (having the .ts code residing in a separate file) is not compatible with lazy-loading? |
I've a .vue + typescript boilerplate maybe it helps :) https://github.com/CKGrafico/Frontend-Boilerplates/tree/vue |
My Vue project is written in Typescript and it uses commonjs as the module system. Can I still use lazy routes? I cannot move to esnext. How can I proceed? |
@mustafaekim if you check the boilerplates is note really difficult, the idea is to compile to es6 using TS and after that add babel to compile to the modules you need |
I cannot use esnext as the module system in tsconfig. And it seems like commonjs does not work well with dynamic imports. |
Version
2.5.2
Reproduction link
https://github.com/fpoliquin/vue-bug-async.git
Steps to reproduce
Create a simple JavaScript or TypeScript component:
Async.ts
Try to load async:
Router.ts
What is expected?
The component should be mounted and operational after loading the webpack chunk
What is actually happening?
The console shows this error:
I found a workaround by replacing a little piece of code :
From src/history/base.js line 341
To
This works for me but I am not sure if it is a good fix for everyone else...
The text was updated successfully, but these errors were encountered: