9

I am using lazy load method to create an backend SPA.

Everything is working smoothing when I navigate the link. Whenever I go to the certain page and reload the it, then Vue will prompt me some error messages.

Uncaught SyntaxError: Unexpected token <

app.js:144 Error: Loading chunk 10 failed.
    at HTMLScriptElement.onScriptComplete (app.js:98)

[vue-router] Failed to resolve async component default: Error: Loading chunk 10 failed.

[vue-router] uncaught error during route navigation:

app.js:15254 Error: Loading chunk 10 failed.
    at HTMLScriptElement.onScriptComplete

This is how my script looks like

const Home = (resolve) => require(['../components/Home.vue'], resolve)
const Category = (resolve) => require(['../components/Category.vue'], resolve)
const CategoryEdit = (resolve) => require(['../components/CategoryEdit.vue'], resolve)
const Register = (resolve) => require(['../components/Register.vue'], resolve)

const router = new VueRouter({
    mode: 'history',
    base: __dirname,
    routes: [
        { path: '/', component: Home },
        { path: '/home', component: Home },
        { path: '/category', component: Category },
        { path: '/category/edit/:id', component: CategoryEdit },
        { path: '/company/register', component: Register }
    ]
});

const nav = new Vue({ 
    router,
    components : {
        Home,
        Category,
        CategoryEdit,
        Register
    },
    ...
}).$mount('#app')

I recorded an GIF for better explanation. https://i.sstatic.net/vXdpC.jpg

1
  • I'm having the same issue! Any solution? Commented Jul 11, 2017 at 23:29

3 Answers 3

12

What's your webpack configuration?

You probably need to set a public path in your output object:

output: {
  path:'/dist',
  filename: '[name].js',
  chunkFilename:'js/[id].[chunkhash].js',
  publicPath: '/',
},
Sign up to request clarification or add additional context in comments.

2 Comments

I wonder how this is done in 2023 where Vite is more and more commonly used.
I had the same issue, thanks. However, you need to double check that the value for public path set / in this case matches with the vue router base value. In this case, you should also set the router base to '/'
0

I have solved this question! You should change

const Register = (resolve) => require(['../components/Register.vue'], resolve)

to

const Register = (resolve) => require(['../components/Register.vue'], m => resolve(m.default))

Comments

-1

I faced the same issue, and what resolved mine is just to rebuild yarn or npm (hope this helps others):

yarn run dev
# OR
yarn run watch

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.