0

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

2
  • You need use <script type="text/x-template" id="vButton"> instead of template tag Commented Oct 16, 2018 at 22:28
  • I tried that but the component doesn't show up at all. Before it showed, just not the text. Commented Oct 16, 2018 at 22:31

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I answered your comment in the answer.
i confirm, it works fine with me like in the screenshot, so i think @omega would have an error that prevents the display
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.