Questions tagged [vue-component]

Component is a powerful Vue feature that allows extending basic HTML elements to encapsulate reusable code.

Components are one of the most powerful features of Vue. They help you extend basic HTML elements to encapsulate reusable code. At a high level, components are custom elements that Vue’s compiler attaches behavior to. In some cases, they may also appear as a native HTML element extended with the special is attribute.

Basic component

Javascript:

// First, we need a vue instance attached to a element
new Vue({ el: '#components-demo' })

// Then we define a new component called "button-counter"
Vue.component('button-counter', {
  data: function () {
    return {
      count: 0
    }
  },
  // The template tag is where we write the component markup
  template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>'
})

HTML:

<div id="components-demo">
  <button-counter></button-counter>
</div>

Vue will then compile the template to virtual DOM and its output will be rendered in the browser to the following:

<div id="components-demo">
  <button>You clicked me 0 times.</button>
</div>

Then for each time you click the button, Vue will execute count++ resulting in count variable updating and triggering Vue re-render the component DOM to show the new value:

You have clicked me 1 times.

Source: https://vuejs.org/v2/guide/components.html

10523 questions
697
votes
16 answers

Vue.js - How to properly watch for nested data

I'm trying to understand how to properly watch for some prop variation. I have a parent component (.vue files) that receive data from an ajax call, put the data inside an object and use it to render some child component through a v-for directive,…
Plastic
  • 9,874
  • 6
  • 32
  • 53
491
votes
21 answers

How to listen for 'props' changes

In the VueJs 2.0 docs I can't find any hooks that would listen on props changes. Does VueJs have such hooks like onPropsUpdated() or similar? Update As @wostex suggested, I tried to watch my property but nothing changed. Then I realized that I've…
Amio.io
  • 20,677
  • 15
  • 82
  • 117
308
votes
9 answers

Vue v-on:click does not work on component

I'm trying to use the on click directive inside a component but it does not seem to work. When I click the component nothings happens when I should get a 'test clicked' in the console. I don't see any errors in the console, so I don't know what am I…
Javier Cárdenas
  • 3,935
  • 4
  • 25
  • 35
304
votes
22 answers

How to add external JS scripts to VueJS Components?

I've to use two external scripts for the payment gateways. Right now both are put in the index.html file. However, I don't want to load these files at the beginning itself. The payment gateway is needed only in when user open a specific component…
Gijo Varghese
  • 11,264
  • 22
  • 73
  • 122
240
votes
18 answers

vuejs update parent data from child component

I'm starting to play with vuejs (2.0). I built a simple page with one component in it. The page has one Vue instance with data. On that page I registered and added the component to html. The component has one input[type=text]. I want that value to…
Gal Ziv
  • 6,890
  • 10
  • 32
  • 43
213
votes
4 answers

Vue 'export default' vs 'new Vue'

I just installed Vue and have been following some tutorials to create a project using the vue-cli webpack template. When it creates the component, I notice it binds our data inside of the following: export default { name: 'app', data:…
rockzombie2
  • 2,835
  • 4
  • 16
  • 20
190
votes
4 answers

Default values for Vue component props & how to check if a user did not set the prop?

1. How can I set the default value for a component prop in Vue 2? For example, there is a simple movies component that can be used in this way: Vue.component('movies', { props: ['year'], template:…
PeraMika
  • 3,539
  • 9
  • 38
  • 63
183
votes
28 answers

[Vue warn]: Property or method is not defined on the instance but referenced during render

The following code has been written to handle an event after a button click var MainTable = Vue.extend({ template: "
181
votes
5 answers

Vuex - Computed property "name" was assigned to but it has no setter

I have a component with some form validation. It is a multi step checkout form. The code below is for the first step. I'd like to validate that the user entered some text, store their name in the global state and then send then to the next step. I…
Connor Leech
  • 18,052
  • 30
  • 105
  • 150
176
votes
6 answers

Passing props dynamically to dynamic component in VueJS

I've a dynamic view:
with an associated Vue instance: new Vue ({ data: function () { return { currentComponent: 'myComponent', } }, }).$mount('#myview'); This allows…
Titouan56
  • 6,932
  • 11
  • 37
  • 61
169
votes
7 answers

Communication between sibling components in Vue.js 2.0

Overview In Vue.js 2.x, model.sync will be deprecated. So, what is a proper way to communicate between sibling components in Vue.js 2.x? Background As I understand Vue.js 2.x, the preferred method for sibling communication is to use a store or an…
S Panfilov
  • 16,641
  • 17
  • 74
  • 96
148
votes
8 answers

Vue.js - Making helper functions globally available to single-file components

I have a Vue 2 project that has many (50+) single-file components. I use Vue-Router for routing and Vuex for state. There is a file, called helpers.js, that contains a bunch of general-purpose functions, such as capitalizing the first letter of a…
Ege Ersoz
  • 6,461
  • 8
  • 34
  • 53