【问题标题】:Vue + Pug - Dynamic attribute names [duplicate]Vue + Pug - 动态属性名称[重复]
【发布时间】:2022-01-01 12:23:46
【问题描述】:

如何使用 Pug 在 Vue 模板中正确添加动态属性名称?我正在使用以下模板:

<template lang="pug">
button(id=`button__${buttontype}`)
    slot(name="text")
</template>

<script>
export default {
    name: "Button",
    data() {
        return {
            buttontype: "primary"
        },
    },
    mounted() {
        console.log(this.test); // This shows the value as 'primary'
    },
};
</script>

正在生成的 HTML 元素是&lt;button id="button__undefined"&gt;&lt;/button&gt;

出于某种原因,buttontype 的值在 id 属性中以undefined 的形式出现。

【问题讨论】:

  • 是的,有效!谢谢!

标签: vue.js pug


【解决方案1】:

尝试使用 v-bind:

<template lang="pug">
button(v-bind:id=`button__${buttontype}`)
    slot(name="text")
</template>

【讨论】:

    【解决方案2】:

    以下模板有效:

    <template lang="pug">
    button(:class="[`button__${test}`]")
        slot(name="text")
    </template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-11
      • 2019-06-05
      • 2012-08-21
      • 1970-01-01
      • 2013-07-23
      相关资源
      最近更新 更多