Transmitting information to the main Vue class component

I'm facing an issue with a Vue component that I've defined using Vue class components. Here is the code snippet:

@Component
export default class MyComponent extends Vue {
  value = 0;
}

My requirement is to create multiple instances of this component as root components, each with different data passed in. I attempted to achieve this by doing the following:

const vm = new Vue({
  el: myElement,
  render: (h) => h(MyComponent),
  data: {value: 1},
});

However, upon inspection, I noticed that the value of this.value in the component remains set to 0 instead of 1. Is there a way for me to ensure that the component is instantiated with the value passed when invoking new Vue?

Answer №1

Your approach is not working because the root component renders MyComponent as a child component, causing a separation in data between the two components. There is no ability to override the data this way.

If you are unfamiliar with Vue class components, here is a simpler solution. Since the MyComponent class extends Vue, you can instantiate MyComponent directly as the root component. Any additional options provided during instantiation will be merged into the base options.

All you need to do is:

const vm = new MyComponent({
  el: myElement,
  data: { value: 1 },
})

No need to define the render function again since it is already included in MyComponent.

Here is a code snippet demonstrating this concept:

const MyComponent = Vue.extend({
  template: '<div>{{ value }}</div>',
  data() {
    return {
      value: 'base'
    }
  }
})

new MyComponent({
  el: '#app',
  data: {
    value: 'extended'
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app"></div>

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

What is the best way to combine a functional component with an HTML string when rendering a node?

I have a functional component that needs to render a child component (which is also functional) and an HTML string sibling to it within the same node. Here is the code snippet: const html = 'My<br>Value'; const ResponsiveLabel = render(&a ...

typescript add some flair to the setter function

I'm attempting to enhance a setter function within a class in the following manner: export class SearchResultSortBy{ private sortByChoice; constructor() { ...} /* getters & setters */ get sortBy() { return this.sortByCh ...

Unable to move draggable table rows in vue.js

I recently utilized vue.js 2.0 to create a draggable table leveraging the capabilities of Vue.Draggable. Even though there are no visible errors, I encountered an issue where dragging tr elements does not work as expected. Interestingly, when trying to dra ...

Steps for clicking on the center of a leaflet map with protractor

I'm currently facing an issue where I am attempting to click on the center of a map located in the second column of the webpage, which has an offset. However, I am encountering a problem where the cursor always points to the center of the page instead ...

Even when there is a change in value within the beforeEach hook, the original value remains unchanged and is used for dynamic tests

My current project setup: I am currently conducting dynamic tests on cypress where I receive a list of names from environment variables. The number of tests I run depends on the number of names in this list. What I aim to achieve: My main goal is to manip ...

Preventing the "Unknown custom element" error in VuePress by excluding custom elements

As I work on documenting a W3C web components library (built with Vanilla JavaScript) using VuePress, I encounter an issue where my "custom" web components trigger an error. This error arises because VuePress mistakenly identifies them as Vue components up ...

What could be causing the Twitter Timeline to fail to load based on a variable in a Vue.js component?

My goal is to display the Twitter timeline of multiple accounts based on the route. I initially attempted to use a plugin called vue-tweet-embed, but encountered issues with it. As a result, I resorted to the traditional method by embedding twitter's ...

Is the concept of client-side authentication, similar to that on single-page applications, simply a facade to deter users who trust in it from tampering with the data on the server side?

Is client-side authentication, like on SPAs, simply a ruse to deter users who believe it from attempting to manipulate the data on the backend, which is protected by real authentication? When we develop SPAs with technologies such as Vue, Vue Router, Reac ...

How can I conditionally disable a button in Vue.js using an if statement?

Can someone help me figure out why all my buttons are getting disabled when I only want one to be disabled? Here is the code where I created a counter with vue.js: <body> <div id="app"> <button @click="co ...

What is the reason for the array length view not updating when a new object is added?

I am dealing with an array of objects and the length is displayed using this code snippet: <strong *ngIf="cart">{{ cart.length }}</strong> Even though when I add items to the array, both the array and its length are showing correctly ...

What is the best method to locate an element<T> within an Array[T] when <T> is an enum?

I've recently started learning TypeScript and exploring its functionalities. I could use some assistance in deepening my understanding. Within our angular2 application, we have defined an enum for privileges as follows: export enum UserPrivileges{ ...

Arranging the output of a Typescript project

I'm currently advocating for Typescript to be implemented in our web application. Can anyone provide insight on the best method for structuring Javascript output files? Should they be excluded from source control? And when it comes to testing, is it p ...

What is the solution for resolving the no-unsafe-any rule?

Currently incorporating TSLint for maintaining the quality of my Angular TypeScript code. I've opted to activate the 'no-unsafe-any' rule from TSLint, as it appears beneficial to avoid making assumptions about properties with type 'any& ...

Enhancing Security and Privacy of User Information with JWT Tokens and NgRx Integration in Angular Application

I'm facing a security concern with my Angular application. Currently, I store user details like isAdmin, isLoggedIn, email, and more in local storage. However, I'm worried about the risks of unauthorized updates to this data, especially since my ...

Animating the navigation toggle in a Nuxt application

When toggling the navigation into view, a simple animation using GSAP is applied. This site is being built in Nuxt/Vue, which is new for me, so I suspect that the issue might be related to how I've organized my functions. The nav toggle button and na ...

Why is it that TypeScript does not issue any complaints concerning specific variables which are undefined?

I have the following example: class Relative { constructor(public fullName : string) { } greet() { return "Hello, my name is " + fullName; } } let relative : Relative = new Relative("John"); console.log(relative.greet()); Under certain circum ...

Tips for eliminating webpack:// from appearing in browser sources

Currently, I am utilizing a webpack template for my Vue.JS website, which can be found here. After deploying the application, everything seems to be functioning properly. However, upon inspecting the developer tools in Chrome and navigating to Sources, I ...

Tips for enabling the "Open in editor" feature in VueDevtools

Whenever I attempt to utilize the "Open in editor" feature of VueDevtools, an error pops up in my terminal stating: Could not open MakeUpStudio.vue in the editor. The editor process exited with an error: (code 1). To specify an editor, specify the EDITOR ...

Position the text alongside the thumbnail rather than in the center

In the current setup, my username text is positioned in the center of the view. I want to reconfigure it so that it displays directly to the right of the thumbnail. However, removing the alignItems: 'center', property from the item disrupts the e ...

Securing important code sections in Node/Express using Typescript: A guide

I'm fairly new to the world of JavaScript/Typescript/Node/Express, and as I've been researching, it seems there isn't a universally accepted method for locking critical sections of code in a Node/Express application. I've come across a ...