繁体   English   中英

动态将值绑定到组件的道具

[英]Bind dynamically value to component's props

我用道具定义了自定义组件。 当我使用此组件时,我需要将值动态绑定到这些道具的一个上

在自定义组件的模板中,我定义了如下元素:

<template>
...
    <div class="input-group-addon" v-show="currency">{{ currency }}</div>
...
</template>

及其道具:

export default {
   ...
    props: {
      currency: {
        type: String
      }
    }
   ...
}

组件在另一个组件中的用法:

组件的模板

<custom-component currency="calculateCurrency" ></custom-component>

组件的代码

export default {
   components: {custom-component},
   data: () => ({
      myProject: null // this is used as v-model in combo box
   }),
   computed: {
      calculateCurrency: function() {
          return myProject.currency; // currency is getter in object myProject
      }
   }
}

所以结果我有这样的事情: 在此处输入图片说明

我也尝试使用

suffix=calculateCurrency

没有引号,但没有帮助。 你能帮我解决吗? 谢谢

我相信您在绑定属性上缺少冒号:

<custom-component :currency="calculateCurrency" ></custom-component>

在“ currency”之前添加它可以进行数据绑定

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM