我需要一些关于 vue 的帮助。
- 是否可以在选项属性中调用数据变量?例如,在“价格”属性中,我想将数据变量称为“税”。
- 以及如何在函数中返回单个选项属性,在我的情况下,函数称为“final”,我尝试返回 this.selected.test,但它不起作用
这是代码:
Vue.component('v-select', VueSelect.VueSelect)
new Vue({
el: '#app',
data: {
selected: null,
tax: 0.07,
mrg: 0.11,
ex_mrg: 0.16,
qnt: '',
options: [{
label: 'Item 1',
id: 1,
price: '* Price: ' + '$' + 0.26 + ' per one printed item',
test: 5,
env: 0.026,
ltrhd: '',
}, {
test: 6,
label: 'Item 2',
id: 2,
price: '* Price: ' + '$' + 7.35 + ' per one printed item',
shrt: 7.351
}, {
test: 7 * 7,
label: 'Item 3',
id: 3,
price: '* Price: ' + '$' + 0.96 + ' per one printed item',
frsb: 0.969269,
yoyo: 0.3658
}, ]
},
computed: {
selectedId() {
return this.selected ? this.selected.id : null;
},
priceId() {
return this.selected ? this.selected.price : null;
},
result: function() {
return this.tax * this.mrg * this.qnt;
},
final: function() {
return this.selected;
}
},
})
<script src="https://unpkg.com/vue@2.5.11"></script>
<script src="https://unpkg.com/vue-select@2.3.1"></script>
<div id="app">
<h1>Please, select item</h1>
<v-select v-model="selected" :options="options"></v-select><br>
<p>Quantity needed:</p>
<input type="number" name="trade-in" v-model.number="qnt" />
<p>{{ priceId }}</p>
<h1>selectedId: {{ selectedId }}</h1>
<p>{{ qnt }}</p>
<p>Final price: ${{ result }}</p>
<p>Final price: {{ final }}</p>
</div>