Vue Custom Element encountering issues when generating independent JavaScript files in the Dist directory

I recently followed the instructions for registering and utilizing custom elements as outlined here:

https://alligator.io/vuejs/custom-elements/

For my project, I am using the standard Webpack template for Vue.

After executing the command

npm run build

I was expecting to generate a packaged web component file named element.js in the dist directory. However, nothing seems to happen. Is there an additional step that I might be missing? The documentation doesn't provide much clarity on this.

The above process led to the creation of the following files within my project structure:

<template>
  <div id="app">
    <example myProp="I can pass props!"></example>

  </div>
</template>

<script>
import Example from './components/example.Vue'

export default {
  name: 'app',
  components: {
    Example
  }
}
</script>

<style>
</style>

main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import vueCustomElement from 'vue-custom-element'

Vue.config.productionTip = false

Vue.use(vueCustomElement);
/* eslint-disable no-new */

import Example from './components/Example.vue';

// Configure Vue to ignore the element name when defined outside of Vue.
Vue.config.ignoredElements = [
    'example-component'
];

// Enable the plugin
Vue.use(vueCustomElement);

// Register your component
Vue.customElement('example-component', Example, {
    // Additional Options: https://github.com/karol-f/vue-custom-element#options
});


new Vue({
  el: '#app',
  template: '<App/>',
  components: { App }
})

components/example.vue

<template>
  <p>Dynamic prop value: {{myProp}}</p>
</template>

<script>
export default {
  props: ['myProp']
}
</script>

package.json

{
  "name": "example",
  "version": "1.0.0",
  "description": "A Vue.js project",
  ...
}  

Upon including the script files for the custom element and Vue libraries, the structure of my index.html file appears as follows:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>example</title>
    <script src="vue/dist/vue.min.js"></script>
    <script src="vue-custom-element/dist/vue-custom-element.js"></script>

  </head>

  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>

</html>

Answer №1

While the docs and an article on alligator.io may not provide a clear explanation, it is important to note that for a vue-custom-element to function properly, you will still need to have both the Vue.js and vue-custom-element libraries present.

Thus, make sure to utilize the <script> tags generated by webpack in your index.html file.


I came across a useful tutorial discussing this topic:

To implement this successfully, ensure that both the core files of Vue.js and the vue-custom-element library are included in your project.

The tutorial also references https://github.com/kartsims/vue-customelement-bundler, a helpful template that consolidates everything into a single .js file.

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

Issue with v-model not connecting to app.js in Laravel and Vue.js framework

Snippet from app.js const app = new Vue({ el: '#app', router, data:{ banana:'' } }); Code found in master.blade.php <div class="wrapper" id="app"> <router-view></router-view> //using Vue ...

Obtain JSON data from an API and display it in a table using axios and Vue.js

I am working with a datatable and trying to populate it with data fetched from an API that returns JSON using the findAll() method from Sequelize. However, when I call the getUser method in console.log, it shows that the data is being retrieved. But when ...

Tips for addressing Navbar collision with body content or other pages using CSS

I have successfully created a navigation bar using CSS and JS. The image below illustrates the example of my navigation bar: https://i.sstatic.net/2l4gj.png The issue I am facing is that when I select "MY ACCOUNT," it displays some content. However, upon ...

Issue with displaying content using v-show in a Nuxt.js menu

Struggling to create a mobile menu with Vue instance error The Nuxt Menu Component : <template> <header id="menu" class="menu-g"> <Nuxt-link to="/"><img src="~assets/logo.svg" alt=&qu ...

Style selector for dynamic drop-down menus

import React, { Component } from "react"; export default class FontChanger extends Component{ constructor(props){ super(props); this.state ={ selectedFont: "", currentFont: "", }; this.handleFon ...

Tips on elevating a dragged div above all other elements when utilizing two scrollbars

Currently, I have two horizontal scrollers where I need to drag a div from the upper scroller to the lower scroller. While this functionality is working properly, there is an issue with the dragged div getting lost under the borders of the scroller during ...

I possess a pair of items that require merging together while combining any overlapping key values in their properties

I have a scenario where I need to merge two objects and concatenate strings if they have the same key. obj1 = { name: 'John', address: 'Cairo' } obj2 = { num : '1', address: 'Egypt' } After merging, the r ...

Struggling to retrieve the header in Angular 6

When setting headers from an Express server written in NodeJS, I use the following code: app.use('/routes', function(req, res, next) { res.setHeader('test', 'test'); next(); ); The headers are successfully sent to th ...

What is the best way to automatically scroll to the bottom of a modal after making a request and

I've been faced with a challenge regarding an online chat widget. My goal is to automatically scroll the widget down to display the latest message when a specific chat is opened. Despite using an async function, I encountered difficulties in achieving ...

Making changes to the Node.js version in a Dockerfile causes issues with installing dependencies

I recently updated the Node.js base image in my Dockerfile from 12.14.1 to 16.14.2, and now I'm encountering issues when trying to build an image with my Dockerfile. I have confirmed that there are no deprecated or removed dependencies being used. T ...

Using jQuery to dynamically load custom post type data in WordPress upon clicking

Let me explain my current project setup. I have developed a custom post type called "People" and have created several individual posts within it. At the moment, I have successfully implemented a modal using JavaScript with static content. Instead of disp ...

Utilizing a nested v-for loop to iterate through data loaded from Firestore and dynamically updating each v-text-field as changes

In my Firebase Firestore collection called Colours, each document includes the attributes 'alpha', 'bravo', 'gamma', and 'delta'. I have set up a nested v-for loop to generate rows of v-text-fields, which successfull ...

Exploring the setTimeout function in JavaScript

As I understand it, the setTimeout function creates a new thread that waits for x milliseconds before executing the JavaScript function. setTimeout(functionName, timeInms); My question is: Is there a way to instruct it to run after all other JS on the pa ...

Verify if the user possesses legitimate Jira REST API credentials

I am currently using the npm module jira-client to send API requests to my Jira system. I need a way to verify whether the user has valid credentials before proceeding. Depending on the validation result, I plan to either: Inform the user that their use ...

Insert PHP File into Division Element

Need help loading a PHP file into a div element without removing existing content? Here's an example: $('.Load_Div').load('example.php'); Let's say the Load_Div already has some content that you want to keep, and you want th ...

Is it possible to capture the bootstrap "data-target" event?

Question Elaborated: I am working on implementing a bootstrap modal, and I want it to be accessible only to logged-in users. Although vue.js allows me to hide this element completely for non-logged in users or manage links effectively, I am facing diffic ...

How to visually represent options without labels using icons in Material UI Autocomplete

My options are structured as follows: const options = ['option1', 'option2']; I am looking to display the options with icons like this: https://i.stack.imgur.com/aubHS.png The current code for rendering options looks like this: ...

Accessing data in form rules with Vuetify: tips and tricks

Is it possible to access a data element within a rule? Click here to see my code in action I'm attempting to change the value of a data element on a text field rule within a Vuetify form. While the rule itself is functioning properly, I'm enco ...

What is the method to retrieve the base host in AngularJS?

I need assistance with the following URL: https://192.168.0.10/users/#!/user-profile/20 When I use $location.host, it returns 192.168.0.10 However, I only want to extract https://192.168.0.10 What is the best way to achieve this? ...

What is the best method to restrict the size of a div to match the dimensions of the

In my webpage, I have a menubar (div) that contains bookmarks. However, when too many bookmarks are added, the menu becomes too wide for the page size I prefer (1280, 720) and becomes scrollable, causing some bookmarks to be out of view. My goal is to ens ...