omega
omega

Reputation: 43843

How to pass data into components in vue.js?

I have this

<template id="vButton">
    <button v-bind:title="name">{{name}}</button>
</template>

<div id="app">
    <ti-button></ti-button>
</div>

js

    Vue.component('ti-button', {
        props: ['name'],
        template: '#vButton'
    });

    var vm2 = new Vue({
        el: '#app',
        data: {
            name : 'hi'
        }
    });

I want the button to have innerText and title attribute to say 'hi'. But it does not. Does anyone know why?

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

Thanks

Upvotes: 0

Views: 40

Answers (2)

Eric G
Eric G

Reputation: 2617

You are doing things mostly right, you just need to pass your data to your prop using a v-bind:

<ti-button v-bind:name="name"></ti-button>

Static values can be passed without v-bind but for dynamic values like you are attempting to pass you need to bind the prop. Check out the static/dynamic prop documentation for more information.

And here is a working demo: https://codepen.io/egerrard/pen/qJpzMQ

Upvotes: 0

Piterden
Piterden

Reputation: 789

Vue.component('ti-button', {
  props: ['name'],
  template: '#vButton'
});

var vm2 = new Vue({
  el: '#app',
  data: {
    name: 'hi'
  }
});
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script type="text/x-template" id="vButton">
  <div>
    <button>{{name}}</button>
    <button>{{$root.name}}</button>
  </div>
</script>

<div id="app">
  <ti-button name="first_button"></ti-button>
</div>

UPD: Do you mean you don't see this? enter image description here

Upvotes: 1

Related Questions