question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vue.js Async Components

See original GitHub issue
  • Laravel Mix Version: 1.4.5
  • Node Version (node -v): 8.5.0
  • NPM Version (npm -v): 5.4.2
  • OS: macOS High Sierra

Description:

Does anyone know how to get Vue.js async components to work? Getting the following error:


Syntax Error: Unexpected token (10:25)

   8 |     el: '#app',
   9 |     components: {
> 10 |         'example': () => import('./components/Example')
     |                          ^
  11 |     }
  12 | });```

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:14
  • Comments:33 (12 by maintainers)

github_iconTop GitHub Comments

142reactions
ankurk91commented, Jan 17, 2019

@ruchern It is specific to webpack (tooling), after all it is babel who will transform the code and will pass to uglify-js. So it is upto end user to configure and enable modern feature.

@pix2D

Here is the complete tutorial -

⚠️ Updated for Laravel Mix v3+

npm install --save-dev @babel/plugin-syntax-dynamic-import
  • Create .babelrc in your project root, and specify the plugin
{
  "plugins": ["@babel/plugin-syntax-dynamic-import"]
}
  • Then in your code somewhere -
// resources/js/routes.js
{
          path: 'post/:id(\\d+)',
         // notice the dynamic import
          component: () => import('./components/post/show.vue'),
          name: 'post.show',
          meta: {
            title: 'Show post',
          }
 }
  • Build
npm run dev

PS If you want to load components via computed property, read this

53reactions
ruchernchongcommented, Oct 8, 2017
components: {
    example: require('./components/Example')
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Async Components - Vue.js
The resulting AsyncComp is a wrapper component that only calls the loader function when it is actually rendered on the page. In addition,...
Read more >
Async Components in Vue 3 - This Dot Labs
Async components are best used in medium to large apps. When an app is formed out of several hundred components, it's wise not...
Read more >
Async Vue.js Components - Vue School Blog
While async components can improve an app loading time by splitting and deferring the loading of their chunks, they could have an impact...
Read more >
How async components can optimize performance in Vue apps
Using async components to build large scale apps is key to maintaining optimal performance. Async components not only ensure your retention rate ...
Read more >
Vue 3 UX Wins with Async Components & Suspense
Lazy loading with Vue 3 async component API ... Vue 3 has introduced the defineAsyncComponent API which makes it very simple to lazy...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found