javascript - VueJs v-bind :type on input element not working not working when using type as property name

标签 javascript vue.js vuejs2 vue-component

代码:

<!DOCTYPE html>
<html>
<head>
  <title>My first Vue app</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/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>

为什么不会:type="type"在这种情况下在这里工作并创建元素,如下所示:<input type="password">

最佳答案

由于您将 type 定义为 prop,因此它不会自动应用于根元素。您需要将其绑定(bind)到您的模板中:

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

任何不被识别为 props 的父作用域属性绑定(bind)都将自动应用于根元素,因此您也可以删除 type prop 并依赖此默认行为:

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

关于javascript - VueJs v-bind :type on input element not working not working when using type as property name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57301421/

相关文章:

javascript - AngularJS 中的子菜单(展开/折叠树)

javascript - React Native 在新初始化的应用程序上找不到模块 ./index

javascript - 为什么 JavaScript 有时被视为低级语言?

vue.js - v-for 使用大括号查找

javascript - 我可以在 Vue.Js 的计算属性中传递参数吗

vue.js - 如何更改node_modules中Vue的src代码进行测试

twitter-bootstrap - 使 bootstrap-vue b-table 'Id' 列不可见

JavaScript:有没有办法自动初始化实时集合中的元素?

vue.js - Vue js - 从对话框获取答案以确认导航 w/Vue Router + Vuetify

vue.js - Vue : render &lt;script&gt; tag inside a variable (data string)