[英]How do I inject a variable into a string within the template?
在我的頁面中,我有:
<ul>
<li
class="validation-labels"
v-for="(label, key) of labels" v-bind:key="key">
{{ constants.validationRequirements[label.type] }}
</li>
</ul>
label
看起來像: { type: string, value: any }
constants.validationRequirements
看起來像:
validationRequirements: {
'capitals': 'Must include {{ value }} capitals',
'minLength': 'Must be a minimum length of {{ value }}'
}
從常量字符串label.value
作為value
注入的最佳方法是什么?
您可以像下面這樣使用:
<ul>
<li
class="validation-labels"
v-for="(label, key) of labels" v-bind:key="key">
{{ validationReqs(label.type, label.value) }}
</li>
</ul>
在您的計算屬性中:
validationReqs(type, value) {
return this.constants.validationRequirements[type]
.replace('{{ value }}', value);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.