在vue中,如何React另一个React对象?

nnt7mjpx  于 2023-04-07  发布在  Vue.js
关注(0)|答案(1)|浏览(180)

作为头衔。
数据结构是

[
  {name: "A city",items:["A district","AB district","ABC district"]},  
  {name: "B city",items:["B district","BB district","BBC district"]}
]

在选择城市时,区的选择也会发生变化,但不起作用。
和默认值:选定也不工作。
我怎么解决?
我试过用ref代替reactive,但不起作用。
谢谢大家。

4c8rllxm

4c8rllxm1#

试着这样做:

<script setup>
import { reactive } from 'vue';
  
const district = [
  {name: "A city",items:["A district","AB district","ABC district"]},  
  {name: "B city",items:["B district","BB district","BBC district"]}
];
const targetCity = reactive({ city: district[0] });
const targetDistirct = reactive({ district: targetCity.city.items[0] });
  
const setFirstDistrictAsDefault = () => {
  targetDistirct.district = targetCity.city.items[0]
}
</script>

<template>
  <select v-model="targetCity.city" @change="setFirstDistrictAsDefault">
    <option v-for="city in district" :value="city" :key="city.name">{{ city.name }}</option>
  </select>
  <select v-model="targetDistirct.district">
    <option v-for="item in targetCity.city.items" :value="item" :key="item">{{ item }}          </option>
  </select>
  <p>
    {{ targetCity.city.name }}<br/>
    {{ targetDistirct.district }}
  </p>
</template>

相关问题