Vue Component Method

Posted on  by admin

Basic troubleshooting

5. Make sure you aren't mutating props (or Vuex state)

  • Modified4 months ago. I have been reading lots of articles about this, and it seems that there are multiple ways to do this with many authors advising against some implementations.
  • To make this simple I have created a really simple version of what I would like to achieve.

I have a parent Vue, parent.vue. It has a button:. In the child Vue, child.vue I have a method with a function:.

I would like to call the sayHello() function when I click the button in the parent. I am looking for the best practice way to do this. Suggestions I have seen include Event Bus, and Child Component Refs and props, etc.

What would be the simplest way to just execute the function in my method? Apologies, this does seem extremely simple, but I have really tried to do some research. 11 gold badge1616 silver badges3030 bronze badges. 3030 bronze badges.

One easy way is to do this:. Simply create a ref for the child component, and you will be able to call the methods, and access all the data it has. 4646 bronze badges. 5,09833 gold badges2929 silver badges4545 bronze badges.

2929 silver badges4545 bronze badges. You can create a ref and access the methods, but this is not recommended. You shouldn't rely on the internal structure of a component. The reason for this is that you'll tightly couple your components and one of the main reasons to create components is to loosely couple them.

You should rely on the contract (interface in some frameworks/languages) to achieve this. The contract in Vue relies on the fact that parents communicate with children via props and children communicate with parents via events.

2. Make sure to update Arrays and Objects correctly (only in Vue 2)

  • There are also at least 2 other methods to communicate when you want to communicate between components that aren't parent/child:.

I'll describe now how to use a prop:. Define it on your child component. Define a trigger data on the parent component. Use the prop on the parent component. Watch testProp in the child component and call sayHello.

Update trigger from the parent component.

Make sure that you always change the value of trigger, otherwise the watch won't fire. One way of doing this is to increment trigger, or toggle it from a truthy value to a falsy one (this.trigger = !this.trigger).

112112 bronze badges. 10.5k11 gold badge2323 silver badges3131 bronze badges.

2323 silver badges3131 bronze badges. I don't like the look of using props as triggers, but using ref also seems as an anti-pattern and is generally not recommended.

Another approach might be: You can use events to expose an interface of methods to call on the child component this way you get the best of both worlds while keeping your code somehow clean.

Just emit them at the mounting stage and use them when pleased. I stored it in the $options part in the below code, but you can do as pleased.


Child component. Parent component. 33 gold badges2020 silver badges4242 bronze badges. 4242 bronze badges. I am not sure is this the best way. But I can explain what I can do.. Codesandbox Demo :

  1. From parent component, send a prop data lets say msg. Have a button at parent whenever click the button toggle msg true/false.

  2. In child component watch prop data msg. Whenever msg changes trigger a method. 11 gold badge99 silver badges1111 bronze badges. 1111 bronze badges. Now it's time to dive deeper into Vue, and create our own custom component — we'll start by creating a component to represent each item in the todo list.

Along the way, we'll learn about a few important concepts such as calling components inside other components, passing data to them via props, and saving data state.

Accessing the DOM using this.$el

  • Note: If you need to check your code against our version, you can find a finished version of the sample Vue app code in our todo-vue repository.

For a running live version, see Familiarity with the core HTML, CSS, and JavaScript languages, knowledge of the terminal/command line. Vue components are written as a combination of JavaScript objects that manage the app's data and an HTML-based template syntax that maps to the underlying DOM structure.

For installation, and to use some of the more advanced features of Vue (like Single File Components or render functions), you'll need a terminal with Node and npm installed.

1. Check that variables are reactive

  • Let's create our first component, which will display a single todo item.

We'll use this to build our list of todos. In your moz-todo-vue/src/components directory, create a new file named ToDoItem.vue. Open the file in your code editor.

Create the component's template section by adding to the top of the file.


3. Use props directly

  • Create a section below your template section.
  • Inside the