0

Code:

<!DOCTYPE html>
<html>
<head>
  <title>My first Vue app</title>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
</head>
<body>
  <div id="app">
      <x :type="type"></x>
  </div>

  <script>
     Vue.component('x', {
         props    : ['type'],
         template : '<input>'
     });

     const x = new Vue({
         el   : '#app',
         data : {
             type : `password`,
         },
     })
  </script>
</body>
</html>

Why wont :type="type" work here in this scenario and create and element like so : <input type="password">?

0

4 Answers 4

2

Since you defined type as a prop, it won't be automatically applied to the root element. You need to bind it in your template:

Vue.component('x', {
  props    : ['type'],
  template : '<input :type="type">'
});

Any parent scope attribute bindings that are not recognized as props will be applied to the root element automatically, so alternatively you can just remove the type prop and rely on this default behavior instead:

Vue.component('x', {
  props    : [],
  template : '<input>'
});
Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to bind once only in component or do I have to do it in component and root element?
The point of props is it allows some data to be passed to a component and then that component can choose to do whatever it wants with that data, in your case you want to assign it to the type attribute on the root element. So it depends on what you want to do. Typically you always need to bind it twice, but in the second example in my answer you don't need to do anything in the child.
2

You need to explicitly specify type prop on template:

Vue.component('x', {
     props    : ['type'],
     template : '<input :type="type">'
});

Comments

0

Try with:

<!DOCTYPE html>
<html>
<head>
  <title>My first Vue app</title>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
</head>
<body>
  <div id="app">
      <x :type="type"></x>
  </div>

  <script>
     Vue.component('x', {
         props    : ['type'],
         template : `<input :type="type">`
     });

     const x = new Vue({
         el   : '#app',
         data : {
             type : `password`,
         },
     })
  </script>
</body>
</html>

Comments

0

You passed the type prop but didn't bind in template. Try a bit the following code

Vue.component('x', {
    props    : ['type'],
    template : '<input :type="type">'
});

const x = new Vue({
    el   : '#app',
    data : {
        type : `password`,
    },
})
<!DOCTYPE html>
<html>
<head>
    <title>My first Vue app</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
</head>
<body>
<div id="app">
    <x :type="type"></x>
</div>
</body>
</html>

2 Comments

I'm confused as to why i have to bind twice, once in Vue.component and again in <x :type="type">, can you please enlighten me?
You are not binding twice. In <x :type="type"> you are just passing the type to x component (can consider a function call like x({type: 'type'})). In x component's template you are binding <input :type="type">. For more see vuejs.org/v2/guide/components-props.html

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.