[英]Parsing Array value as props in Vue.JS
我有以下組件用於父級中的“選擇”選項:
<template>
<label class="label" for="select">{{ label }}</label>
<select class="form-control form-control" v-bind:name="name" id="select">
<option v-bind:options="options" v-for="(value, index) in options" :key="index">{{ value.objectValue }}</option>
</select>
</template>
export default {
name: "Select",
props: {
label: String,
options: Array,
name: String,
objectValue: String
}
}
我解析的數組由對象組成,如下所示:
let array = [{name: valueOne}, {name: valueTwo}]
我的問題是當我通過道具“objectValue”時它沒有 select 它。 這就是我使用組件的方式:
<Select label="Select country" objectValue="name" v-bind:options="array"/>
所以我得到一個空白數組,但值的數量正確。 所以問題應該圍繞從 objectValue 道具解析“名稱”值。
select
需要一個具有初始值的v-model
。
聲明一個數據屬性(名為selectedOption
),並將<select>.v-model
綁定到它:
<template> <select v-model="selectedOption">...</select> </template> <script> export default { data() { return { selectedOption: null } } } </script>
在objectValue
上添加一個觀察者,將新值復制到selectedOption
:
export default { watch: { objectValue: { handler(newValue) { this.selectedOption = newValue }, immediate: true, }, } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.