Lazy loading components in Vue enables the components on the page

While attempting to implement lazy-loading for a module within my custom Vue application, I encountered a problem.

The structure of my routes is derived from a database, and one of them pertains to displaying programs for children. In this program view, I have multiple components being loaded such as lists of clients and consents.

This is how it currently looks:

<div role="tabpanel" class="tab-pane" :class="{'active': current_tab == 'consent'}" id="consent" v-if="displayConsentComponent">
    <consent v-model="consents" :edit="edit"></consent>
</div>

// Components are imported like this:

<script>
    import Consent         from './_consents';
    import AssignedStaff   from './_assigned-staff';
        ......
    export default {
        components:{
            Consent,
            AssignedStaff,
            .....
        },
        data(){
            return {
                  ..........
            }
        }
    }
</script>

I wanted to modify it according to your method, so I made the following changes:

<div role="tabpanel" class="tab-pane" :class="{'active': current_tab == 'consent'}" id="consent" v-if="displayConsentComponent">
    <consent v-model="consents" :edit="edit"></consent>
</div>

// Updated imports:

<script>
    const Consent       = () => import('./_consents');
    const AssignedStaff = () => import('./_assigned-staff');
        ......
    export default {
        components:{
            Consent,
            AssignedStaff,
            .....
        },
        data(){
            return {
                  ..........
            }
        }
    }
</script>

However, upon making these modifications, my entire application fails to build and run after rebuilding. Alternatively, I receive the following error:

Error Message: Unexpected token (58:8). It seems like an appropriate loader might be required to handle this file type.

I've updated all node packages, including Vue to the latest version (2.5.1X), but unfortunately, that did not resolve the issue. Any insights or assistance would be greatly appreciated.

Thank you in advance, Gabriel

Answer №1

Perhaps relocating the async component to the components section could solve the issue. For more information, you can refer to: https://v2.vuejs.org/v2/guide/components-dynamic-async.html#Async-Components

<script>
    
    export default {
        components:{
            Consent: () => import('./_consents'),
            AssignedStaff: () => import('./_assigned-staff'),
            .....
        },
        data(){
            return {
                  ..........
            }
        }
    }
</script>

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

Jquery UI Dragging Feature Disabled After Element Dropped

Here is the code I have written: <!doctype html> <html lang="en"> <head> <meta charset="utf-8> <meta name="viewport" content="width=device-width, initial-scale=1> <title>jQuery UI Droppable - Default function ...

How to extract various elements from a database array by utilizing .map within a React Component?

I'm currently diving into React and have encountered a challenge when it comes to rendering multiple elements simultaneously. The issue revolves around fetching an Array from Firebase, which is supposed to generate a new div and svg for each item in ...

Tips for syncing the drag motion of two frames using jQuery

Currently, I'm developing a whack-a-mole game using javascript/jQuery. My goal is to allow users to drag the mole holes to their desired positions on the screen and then click start to begin playing (with moles popping out of the holes). However, I am ...

Trigger a JavaScript function upon clicking a link

Is it possible to have a CMS that loads articles using ajax, where the article is loaded through a function with parameters, and when a certain link is clicked, it redirects to the target page and launches the function on that page? For instance, let' ...

Tips for testing views in ember.js using unit tests

We are currently delving into the world of Ember.js. Our development process is completely test-driven, and we aim to apply the same methodology to Ember.js. Having prior experience in test-driven development with Backbone.js apps using Jasmine or Mocha/Ch ...

Challenges associated with utilizing img src in JavaScript

I am facing a simple issue. I have carInfo data retrieved from a JSON file, but I am struggling to correctly parse the img source stored in the variable $imgsrc instead of treating it as a string called "$imgsrc". This data needs to be appended to my HTML ...

Utilizing jQuery to generate dynamic nested divs within the ul li tags

I am looking to dynamically generate nested divs within a ul li. The goal is to create the following structure programmatically: <ul> <li> <div class="videoThumbnail"> <img src="./img6.jpg" /> &l ...

What are the steps to integrate MaterializeCss into Vue.js?

I prefer not to utilize Vue-Material or Vuetify. My choice is Materialize. Here's what I do: npm install materialize-css@next In my main.js file, where I define my new Vue App, I import Materialize like this: import 'materialize-css' Th ...

Is storing Vuex state in sessionStorage a secure practice?

I am currently developing a web app with Vue.js/Vuex, but I am facing an issue where the state is lost when the user refreshes the page. I have attempted to address this by persisting some states in sessionStorage, however, I have come to realize that use ...

Stop the print dialog box from causing the page to refresh

I have implemented a print button for an invoice: </script> <!--Function for printing invoice--> <script> function printpage() { window.print() } </script> <button id="print" name="print" class="btn btn-info" onClick="pri ...

What is the best way to create an update function that allows an edit form to replace existing text with new content?

I'm struggling with creating a straightforward update function to edit an ingredient and save the changes. Despite my efforts, I can't seem to get it right. Every time I submit the form, I just end up with the same unchanged ingredient. Here is t ...

Display all dates within a specific range using the indexOf method

I am working on creating a datepicker in vue3. As part of this, I want the days between two selected dates to be highlighted when hovered over. I have attempted to achieve this using the "indexOf" method, but unfortunately, I am not getting the desired res ...

What is the benefit of storing an IIFE in a variable?

When it comes to using IIFE in JavaScript and AngularJS, I have come across two common structures: Structure 1: //IIFE Immediately Invoked Function Expression (function () { }()); However, there is another structure where the IIFE is assigned to a var ...

Utilizing a function as an argument in another function (with specified parameters)

I’m stuck and can’t seem to solve this problem. In my function, the parameter filter needs to be a function call that accepts an object created within the same function: function bindSlider(time, filter) { var values = { min : 8, max : ...

Is it possible to execute an Ajax Request using JQuery?

I've been attempting to update the content within a <div> using a JQuery Ajax Call, but unfortunately I'm encountering some difficulties. Whenever I click on the onclick <a> element, nothing seems to happen. Below is the code that I a ...

How about utilizing node.js as a peer for WebRTC?

Are there any available modules for utilizing node.js as a peer in WebRTC? I am interested in using WebRTC in a client/server manner rather than P2P for its ability to send packets unreliably (i.e. avoiding the delay caused by TCP's guarantee of packe ...

Arrows within modal image (adjusting size based on image)

In my modal carousel, the position of the arrows changes based on the image resolution. I would like the arrows to always remain inside each image, regardless of resolution. ...

The YouTube video continues to play even after the container is closed

I recently worked on implementing a lightbox feature using jQuery, where a window opens upon clicking an element. Everything was working fine with images, but when I tried to open a YouTube video and play it, the video kept playing in the background even a ...

I am looking to personalize a Material UI button within a class component using TypeScript in Material UI v4. Can you provide guidance on how to achieve this customization?

const styling = { base: { background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)', border: 0, borderRadius: 3, boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)', color: 'white', height: 48, ...

Ensure that any portion of a child DIV becomes visible only if it falls within the confines of the parent DIV, even if the overflow:hidden property is

My inner div can be resized by the user, and I have it inside another div. The issue is that when the inner div extends beyond the outer div, it covers it completely, or I have to use overflow:hidden, which hides the outer edges of the inner div. Ideally, ...