javascript - Vue 绑定(bind)覆盖元素属性

标签 javascript vue.js vuejs2 vue-component

我有一个呈现 HTML 输入的组件:

<input
    :placeholder="placeholder"
    v-model="value"
    type="text"
    :disabled="disabled"
    :readOnly="readOnly"
    @focus="onFocus"
/>

请注意,类型不是绑定(bind)/响应式(Reactive)的。

当我将此组件放入另一个组件中并将对象绑定(bind)到它时,type 会被覆盖。

 <my-input v-bind="{type: 'foobar'}"></my-input>
<小时/>

这是设计使然还是错误

<小时/>

示例(检查 HTML 中的input[type]):

const Input = {
    template: '<input type="text"/>'
    //                      ^^^^ "text" gets overriden to "foobar"
}
new Vue({
    el: '#demo',
    components: {
        'my-input': Input
    }
});
<script src="http://vuejs.org/js/vue.min.js"></script>
<div id="demo">
    <my-input v-bind="{type: 'foobar'}"></my-input>
</div>

最佳答案

我在一个问题中回答了这个问题,这是预期的

https://github.com/vuejs/vue/issues/5846#issuecomment-307098682

但是,您可以通过将 attr 添加为 props 来忽略它们并忽略它们

const Input = {
    props: ['type'],
    template: '<input type="text"/>'
    //                      ^^^^ "text" won't get overriden
}
new Vue({
    el: '#demo',
    components: {
        'my-input': Input
    }
});

其他属性(如class)被合并,但type只能被覆盖

关于javascript - Vue 绑定(bind)覆盖元素属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44431754/

相关文章:

javascript - Angularjs 获取范围属性而不在自定义指令中使用隔离范围

javascript - 在javascript中使用 'this'自身内部相同对象的键值对

laravel - [Vue警告] : Error in v-on handler: "TypeError: Cannot read property ' fire' of undefined"

javascript - 在动态组件中仅安装一次触发器

asp.net - 寻找简单的基于网络的表单设计器的示例

javascript - 使用 JavaScript 的 split 将字符串切碎并放入两个数组中

vue.js - 当我更改页面时 Nuxt $vuetify.theme.dark 重置

javascript - VueJS 2 v-for表单输入单选按钮检查属性不起作用

javascript - 如何以编程方式启动 Vuetify 对话框并等待响应

laravel - 如何在 Laravel 5.4 中加载 Vue 组件?