【问题标题】:Are custom attribute bindings possible with Vue templates?Vue 模板可以自定义属性绑定吗?
【发布时间】:2017-11-26 07:34:19
【问题描述】:

我正在尝试在我的 Vue 模板中绑定自定义属性值。我怎样才能做到这一点?

(编辑:以下代码实际上正确绑定。第三方库(基金会)干扰了绑定。留下问题,因为它可能对其他人有用。

<template>
    <span v-bind="{ 'aria-controls': inputControlId }"></span>
    <input v-bind="{ 'id': inputControlId }">
</template>

<script lang="ts">

    import Vue from 'vue';
    import Component from 'vue-class-component';

    @Component
    export default class Slider extends Vue {
       inputControlId = "TheBourneId";
    }
}
</script>

【问题讨论】:

    标签: vue.js vuejs2 vue-component


    【解决方案1】:

    binding attributes 的常用语法是

    <template>
        <span v-bind:aria-controls="inputControlId"></span>
        <input v-bind:id="inputControlId">
    </template>
    

    还有一个shorthand

    <template>
        <span :aria-controls="inputControlId"></span>
        <input :id="inputControlId">
    </template>
    

    您可以使用问题中的语法一次绑定multiple properties,它在classstyle 之外不常用,尤其是对于单个属性。

    听起来真正的问题是您的 CSS 框架。

    【讨论】:

    • 谢谢!在尝试了一些变体之后,我发现第 3 方库(基金会)在我的案例中干扰了绑定。我的示例代码实际上按预期工作,但使用您的语法似乎更好。我会更新问题以反映这一点。
    • @pulekies 没错,您正在使用第三种语法,但它主要用于同时绑定多个属性时。我会更新答案。
    猜你喜欢
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多