Lazy loading in Vue.js is a feature that allows components

I've been using Laravel mix to compile my Vue.js components, and now I want to implement lazy loading for my components. I found a helpful resource here:

https://v2.vuejs.org/v2/guide/components.html#Async-Components

However, when I attempt the following code:

Vue.component('setting', () => import('./components/settings/setting.vue'));

I encounter this error message:

 error  in ./resources/assets/admin/vue/core.js

Syntax Error: Unexpected token (36:31)

  34 |  */
  35 |
> 36 | Vue.component('setting', () => import('./components/settings/setting.vue'));
     |                                ^

What could be causing this issue? Interestingly, when I register the 'normal' component, everything works fine.

Answer №1

After taking into account the feedback provided in the comments, it is important to specify the publicPath in your webpack.mix.js file for proper functionality. Below is a condensed version of a functional webpack.mix.js file that has been tested on Laravel 5.5 with Laravel-mix:

const { mix } = require('laravel-mix');
mix.setPublicPath('public/');

mix.webpackConfig({
    output: {
      chunkFilename: 'js/[name].[chunkhash].js',
         publicPath: '/'
     },
    module: {
        rules: [{
            test: /\.js?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            options: {
              presets: ['babel-preset-env'],
              plugins:['babel-plugin-dynamic-import-webpack']
            }
        },
  ]
    }
});

mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css')
.version();

If your manifest.json was generating incorrect paths, it may have caused webpack to not locate the appropriate chunk file paths. You can either adjust the paths accordingly or simply stick with the default paths as shown above. The provided code will generate app.js in the public/js directory along with the chunked files. Additionally, it will create the manifest.json in the public directory. Remember to reference your app.js file in your blade file using:

<script src="{{mix('/js/app.js')}}"></script>

Ensure that you have installed the babel loader and babel dynamic import plugins, with the former being necessary for utilizing babel plugins in webpack. It is uncertain whether Laravel Mix reads the .babelrc file, so having these plugins installed should suffice.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for refreshing data in BigQuery following an onUpdate occurrence

Currently, I am successfully importing data from Firebase to BigQuery using the onWrite event and the table.insert function. However, I am facing an issue when trying to update data in BigQuery on the onUpdate event as the table.update function is not av ...

Discover the method for utilizing innerHTML using Id or class with php echo and fetching values from a database

As an alternative to the validation code value, I am utilizing JavaScript to dynamically replace it upon button click. Here is the snippet of code: document.getElementById('showCode').addEventListener('click', function() { const ic ...

XMLHttpRequest is unable to load due to the absence of an 'Access-Control-Allow-Origin' header on the requested resource

No matter how many different solutions I've attempted to implement from various sources, none seem to be working for me. Here's a snippet of code that I'm trying to execute within the setInterval method in the document.ready() function. f ...

Displaying comments on a Laravel post is essential for keeping users engaged and

Yesterday, I managed to successfully count the comments from a post. Now, my goal is to display these comments in the admin dashboard. Here is the code snippet from my controller: public function getComment() { $user = Auth::user(); $posts = $user ...

Is there a way to implement a timeout for redirection or new pages opened on a different server?

Is there a way to redirect my page to another website, like google.com, or to open google.com as an additional page in the browser? In addition, is there a way to close any previously opened google pages (if opened as a new page) or return to my page (if ...

Looking to incorporate multiple accordion drop down menus on your website? Utilize a combination of HTML, CSS, and JavaScript to

I am experiencing a challenge with implementing multiple accordion menus on my website. Whenever I attempt to duplicate the code, the new accordion menu appears but clicking on the first bar simply scrolls me back to the top of the webpage. Below is the H ...

"Troubleshoot: Inadequate performance of table search in node.js due

Embarking on a journey of learning here excites me. I have an insatiable appetite for new knowledge! Grateful in advance for any assistance! Presenting a concise Node.js JavaScript code snippet of 30 lines for a standard table search process. The "table" ...

Getting the right AngularJS $scope within a controller for beginners

I am struggling to understand how to toggle the visibility of an element on a webpage using a controller. Below is a code snippet with an example - you can test it by clicking the "close edit" button (nothing happens): var appModule = angular.module(&a ...

Automatically scrolling a div to the bottom in a React component

I am currently working on developing a chat application for my school project, and I would like to implement the feature that automatically scrolls to the bottom of the chat window. Despite trying various solutions and encountering React errors, I have not ...

Ways to create a clickable anchor tag without using any text

I am currently designing my own website and incorporating links to various social media platforms using icons. However, I am facing an issue where the links are not clickable. For a detailed look at my problem, you can refer to this JSFiddle: http://jsfid ...

Retrieve the JSON array value from the observable and pass the information to the frontend

Attempting to retrieve a JSON array from a json-server using observables, then passing the value to the frontend for search functionality on the received JSON array. Created a service and utilized HTTP get method to establish a connection with the server ...

Issue encountered with Vue Templates: Component mounting failed: template or render function is undefined

While working on creating Vue Templates, I came across this persistent error: Failed to mount component: template or render function not defined. After researching, it seemed that including the full Vue library might solve the issue. Therefore, in my boo ...

What is the reason that prototypes are not passed down through inheritance?

Can anyone shed light on why Validator.js is structured the way it is initialized (as shown in the first code snippet) and the reason behind the inability to call the validate method (as demonstrated in the second snippet)? I am currently experimenting wi ...

Utilizing AngularJS to implement a Currency Filter on the output of a personalized filter

I'm currently working on a custom filter that transforms zero values into ' - ' for display purposes. The goal is to display currency formatting for non-zero values. However, I encountered an unexpected token error while trying to implement ...

Tips on keeping a div element in a fixed position until triggered by jQuery

I've managed to create a navigation bar within a header that sticks to the top of the screen once you scroll down past 100px. The functionality works well, but I'm looking to make the navigation bar stay fixed on the right until it snaps, essenti ...

Maintain the query parameters while utilizing Link or Inertia.get()

Utilizing the "preserve-state" option in Inertia is a convenient way to maintain the state of a component. However, a drawback is that while preserve-state successfully retains form contents, it does not retain query strings. This becomes apparent when fi ...

Turn off SSL on your Node.js/Express app running on Heroku

Once I've deployed to Heroku, the URLs are automatically converted to https. Is there an easy method to switch them back to http? ...

Discovering track data and obtaining a client_id for SoundCloud

I'm currently attempting to set up a streaming music feature using SoundCloud for an HTML5 player, and ideally it should function similar to this sample provided in the following fiddle http://jsfiddle.net/kS97M/5/ SC.initialize({ client_id: ' ...

Bringing in specific functionalities from rxjs in an Angular 2 component

I am currently working on an Angular 2 project that was initialized using the Angular CLI. My main goal is to ensure that the initial load of the project is as fast as possible. To achieve this, I have been focusing on reducing the sizes of all the bundles ...

Implementing Button Activation and Deactivation Upon Checkbox Selection in JQuery

When a single checkbox is selected, the Edit and Delete buttons are enabled while the Add button is disabled. If two or more checkboxes are selected, the Delete button is enabled while the Add and Edit buttons are disabled. This is my HTML code: < ...