Vue: Component designed exclusively for select pages

In my Symfony project, I am utilizing Vue.js (2) and I currently have a Vue instance with "global" components (specifically an Editor component with some CKEditor libraries) precompiled using the default symfony webpack setup.

//app.js
import Vue from 'vue';
import Editor from './components/Editor';

new Vue({
    el: '#app',
    components: {Editor}
});
//components/Editor.vue
<template>
    <div>
        <textarea id="editor"></div>
    </div>
</template>

<script>
import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
//tons of other imports 

export default {
    name: "Editor",
    mounted() {
        ClassicEditor.create(document.querySelector('#editor'), 
            plugins: [
                Essentials,
                Heading,
                //more plugins used from imports
            ],
            toolbar: [
                'heading',
                //more plugins
            ],
        }).then(editor => {
           
        }).catch(error => {
           
        });
    }
}
</script>

Currently, my Editor.vue is being loaded globally by default because it is included in the Vue instance along with every other component. However, this results in unnecessary loading of the editor on every page request, even if it's not needed.

The editor file has numerous imports due to customization, making the javascript file quite large and slowing down loading times.

My objective is to eliminate the global component Editor and instead embed it in a separate javascript file called editor.js, which can be included only on specific pages as needed.

I am unsure about how to achieve this as my Vue instance is created in app.js and I need to somehow add the Editor component in the editor.js file.

Answer №1

To simplify things, just create an Editor component as an async component

With async components, Webpack automatically bundles them into separate js chunks and loads them on demand when necessary...

You can either register the component locally (in each component where you need it):

export default {
  name: "ComponentUsingEditor",
  components: {
    'Editor': () => import('components/Editor.vue')
  }
)

or globally:

app.js

import Vue from 'vue';

Vue.component(
  'Editor',
  () => import('components/Editor.vue')
)

new Vue({
    el: '#app'
});

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

Changing a numeric string into a number within an Angular 2 application

Looking for advice on comparing Angular 2 expressions. I have an array of data stored as numeric strings in my database and I need to convert them to numbers before applying a condition with ngClass for styling purposes. Any suggestions on how to perform ...

Flask API returning error: Missing 'Access-Control-Allow-Origin' header for POST requests

I've been diving into my first API project and so far have relied on Google for assistance. However, I've hit a roadblock with the following issue: While my POST requests are successful in Postman, using a form in my HTML to make a POST request ...

Annotorious in Vue.js Template: A Step-by-Step Guide

I am trying to implement annotorious (with openseadragon plugin) in a vue.js (vue 3) template. I have successfully installed annotorious using npm. Below is the code snippet I have written so far: <template> <div class="flex-grow& ...

JavaScript button click changes the selected row's color in a DataTable

I am looking to dynamically change the color of a row in my Datatable when a specific button is clicked. Here is the HTML code snippet for the rows: <tr role="row" class="odd"></tr> <tr role="row" class="even selected"></tr> & ...

The context environment is failing to update the current state

Working with context in React can be tricky for some, including myself. I was hoping the new Context API would make things easier, but I'm still facing some issues. While I can get the initial value to display, the updates based on my Effect are not r ...

what is the proper method to fill a hidden input using Vuejs 2?

I'm trying to figure out how to pass the value returned by this function to a hidden input field. I attempted using the v-model directive, but it didn't work. How can I solve this issue? Here is the computed function: computed:{ curren ...

Altering icons using timeouts in a repeating sequence

So, I have this code that is supposed to loop through each record in a table and change the save icon to a checkmark for 2 seconds before changing it back. However, it's not working as expected. I've used a similar method for other buttons which ...

Certain browsers have difficulty running CSS keyframes animations

As I work on my website, I have integrated a CSS keyframes animation that is triggered by clicking a link connected to a JavaScript function. Here is an excerpt from my CSS file: #banner { background-color: #00BBD2; position: absolute; width: 100%; heigh ...

What are the steps for combining AngularJS with Java JAAS authentication system?

My web application combines AngularJS on the frontend with Java on the backend. Angular communicates with the Java backend through Restful webservices exchanging JSON data over HTTP. I am in need of developing an authentication mechanism for this app and I ...

haphazardly showcase circular shapes within a confined space

Is there a way to display circles randomly inside a box without them extending beyond the box or touching its corners? Can the circles be plotted using relative values (percent) within the div? For example, can a circle be placed 10% from the left side and ...

Canvas with a button placed on top

I am working with a canvas and trying to position an HTML element over it using CSS. My goal is for the button to remain in place on the canvas even when resizing the entire page. Here is the code I am currently using. https://jsfiddle.net/z4fhrhLc/ #but ...

The click event is triggering before it is able to be removed by the code preceding the trigger

Here's a scenario I recently experienced that involves some code written using JQuery. It's more of a statement than a question, but I'm curious if others have encountered this as well: <input type="submit" value="Delete" o ...

Edit the package.json file in a Vue project to enable custom build configuration

Is this the right method to update my Vue 2 package.json serve script for compilation in a new custom build mode called "devserver"? "scripts": { "serve": "vue-cli-service lint --fix && vue-cli-service serve --open --mo ...

npm is unable to install a forked git repository in its current state

Attempting to install a customized version of ng2-smart-table on my application, but npm seems to be struggling with the process. I've experimented with various commands such as npm install git+http://github.com/myusername/ng2-smart-table.git npm i ...

Navigating through a jQuery array while utilizing the $.inArray method

Below is an example of my Array that I am working with, retrieved from a successful jQuery .post call: data = JSON.parse(data); $(".chk_permission_").each(function() { if ($.inArray($(this).val(), data)) { $(thi ...

File bootstrap.min.css is currently experiencing compatibility issues

I am currently working on a website where I have two images that are displaying vertically. However, I would like these images to be displayed horizontally with animation. I attempted to use Bootstrap to achieve this horizontal layout, but when I include ...

Expanding and shrinking the index within a specific circular boundary in JavaScript

I'm dealing with a circular range of ASCII values from a to z, where each letter corresponds to a number between 97 and 122. I have no issue with increasing the value within this range, but I am struggling when it comes to decreasing it. For example ...

ordering the date and time in a reverse sequence using javascript

I am working with dynamically generated date and time data and I am looking to sort them in descending order using JavaScript. Here is an example of the data format I am dealing with: var array= ["25-Jul-2017 12:46:39 pm","25-Jul-2017 12:52:23 pm","25-Ju ...

A step-by-step guide on how to substitute document.write with document.getElementById('ElementID').innerHTML

As someone who is not a programmer, I often find myself attempting to grasp code and make adjustments through trial and error. Currently, I am faced with the challenge of modifying the document.write function in the function pausescroller within a Joomla m ...

Automatically open the Chackra UI accordion upon page loading

Currently, I am working on developing a side menu with accordion functionality in Nextjs. I have managed to get the index values from 0 to 1 based on the page URL, but I am facing an issue where the accordion is not opening as expected. Each time a user ...