Is it possible to attach a Vue component to more than one div element simultaneously?

import Selector from '@components/contactSelector'
let vueInstance = new Vue({
  components: { Selector },
  data () {
    return {
      url: url,
      isFilter: false,
      type: 'external',
      selectedList: []
    }
  },
  render (h) {
    return h(Selector, { props: { url, selectedList: this.selectedList, maxHeight: '300px' } })
  }
})

I have an instance of Vue created above. Is it possible to render it on multiple divs? I have attempted to do so but it does not seem to work. Is there a way to mount it on multiple divs or is it intended to be mounted on a single div only? If anyone has knowledge about this, please provide assistance. Thank you.

vueInstance.$mount('#contactSelectorId')
vueInstance.$mount('#contactSelectorId2')

Answer №1

In my opinion, the desired outcome can be accomplished by establishing a root component that incorporates your component as a child element.

If you require a single component to appear multiple times on the same page, follow these steps:

Main component:

import ChildComponent from './ChildComponent';

export default {
    components: {
        ChildComponent
    },

    :
    :

Afterwards, you can easily include it (<child-component/>) wherever needed.

Keep in mind that in this scenario, the new parent component should be assigned to your vueInstance to serve as the root.

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

Discover various ways to encapsulate Vue.js components

I am currently in the process of developing a website that utilizes classic multiple pages powered by blade templates. However, I am looking to incorporate vuejs2 components for more dynamic functionalities. Although things are running smoothly, I am faci ...

Is it possible to prefetch library calls in Next.js in advance?

Recently, I started working with GeoChart using react-google-charts (https://github.com/RakanNimer/react-google-charts). However, I noticed that several scripts load after the entire process is completed. In my scenario, is loading towards the end. Is t ...

The rule "react/jsx-sort-props" does not have a valid configuration

I've been attempting to organize props names alphabetically using the eslint-plugin-react plugin but I keep encountering this error: [Error ] .eslintrc.json: Configuration for rule "react/jsx-sort-props" is invalid: Value {"callbacksLast":true,"shorth ...

What is the proper way to reference a computed symbol that has been inherited as a function in an extended class

As a newcomer to Typescript, my understanding of the code might be lacking. I am currently working with the Klasa framework, which is built on top of Discord bot framework using Discord.js. The framework recently introduced plugin functionality and there a ...

What is the best way to send props from a child component to its parent in a React

I'm a beginner in React and I'm attempting to develop a "CV-Generator" similar to the one shown here. In this application, whenever a user inputs data in any of the input fields, it is automatically displayed in the render preview on the right si ...

What is preventing me from binding ng-click to my element?

I've encountered an issue where the ng-click event is not activating on my element. Despite using the in-line controller script as shown below, I am unable to trigger my alert. Are there any integration issues that I should be mindful of? What could p ...

Using React.JS: Display Object Map in rendering

I have an object with family information that looks like this: Family: Parent0: BirthPlace: "dsa" Birthday: "2021-01-04" Relationship: "dsa" SurnameAndName: "dasdsa" Parent1: BirthPlace: & ...

Using the setTimeout function within the created lifecycle hook in Vue.js

methods: { displayMessage() { console.log("Hello World"); } }, created() { setTimeout(this.displayMessage(), 5000) } I have my code snippet here. I am trying to use setTimeout in the created hook, but the browser is not recognizing ...

Node.js socket.io emit function not functioning properly

In my current configuration, I have the following setup: app.set('port', process.env.PORT || 3000); var httpserver = http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port ' ...

The Protractor option is nowhere to be found on the Run Configuration popup window in Eclipse

Issue with Protractor in Eclipse: Unable to locate Protractor option on Run Configuration popup. Despite following the steps outlined in http://www.protractortest.org/#/ and this guide on configuring Protractor with Eclipse (specifically the 2 Answer step ...

Pass information to a PHP file using JavaScript when the form is submitted

Essentially, I am looking to extract values from various inputs and spans using JavaScript when the submit input is clicked. These values will then be sent to PHP using $post in order to ultimately send them via email. Previously, I tested replacing all of ...

The PHP file on the server is missing the mandatory "event" parameter for the EventSource

Explaining this issue was a bit of a challenge. I've set up a Javascript EventSource object with some customized event handlers like so: var source = new EventSource('updates.php'); source.addEventListener('add', addHandler, fals ...

Steer Your Way: How to Redirect to a Different Router or Middleware in Node.js and Express.js

I'm creating an application using VENoM stack, and I have set up some middleware in the API like this: const express = require('express'); const router = express.Router(); require('./routes/orderRoutes')(router); require('./ ...

When attempting to send data using jQuery to PHP, an issue arises where PHP is unable to receive the information, resulting in an undefined variable

Has anyone successfully sent data from an HTML select to a PHP file using jQuery? I've tried multiple solutions suggested here, but none seem to be working for me. Below is the code I'm using: <select id="city" name="city" > <optgro ...

Troubleshooting Issue with Ionic's UI Router

Recently, I created a basic Ionic app to gain a better understanding of UI router functionality. To my surprise, when I ran the app, nothing appeared on the screen. Additionally, the developer tools in Google Chrome did not show any errors or information. ...

Using the foreach Loop in Javascript and AngularJs

Having trouble with a foreach loop because you're not sure of the column name to access specific data? Here's a solution to display all columns along with their corresponding data: angular.forEach(data, function(value, key) { console.log( &a ...

Capture all URLs containing [...slug] and generate static props only for valid addresses in Next.js

I am utilizing dynamic routes with the fallback:true option to ensure newly created pages are accepted. First, I check if the parameters are true, then I create related props and display the component with those props. In the console, I can observe that Ne ...

Adjusting the border-right size based on scroll position

Apologies if this question seems trivial, but I am completely new to coding in HTML and JavaScript. I understand that I can make some changes based on scroll position, but right now I'm a little confused. I want to increase the height of a box as the ...

Is there a way to prevent the text in my text boxes from staying there when I refresh the page?

Currently working on an HTML5 project with Javascript, here is a snippet of my code: Your inquiry <textarea type="text" name="phrase" id="phrase" cols="50" rows="5" placeholder="Write your text here. . ."></textarea> I am looking for a way ...

AngularJS dropdown not reflecting changes when using ng-selected

While working with AngularJS, I have encountered an issue where the first child of the select element remains empty despite my efforts to populate it. Below is the HTML code snippet: <select id="connectTV" aria-label="How many Dish TVs" ng-model="conn ...