簡體   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