Vue3父子组件双向绑定值用例

javascript 复制代码
<template>
  <div>
    <MySelect v-model="data" />
    <div>
      code1: {{ data.code1 }}
    </div>
    <div>
      code2: {{ data.code2 }}
    </div>

  </div>
</template>

<script setup>

import { countdownEmits } from 'element-plus';
import MySelect from './component/mySelect/index.vue';
import { ref, reactive, onMounted } from 'vue';

let data = ref({
  code1: '1',
  code2: 'Option2',
});

onMounted(() => {})


</script>
<style></style>

javascript 复制代码
<template>
  <div>
    <el-select v-model="localValue.code1" @change="change">
      <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
    </el-select>
    <el-select v-model="localValue.code2" @change="change">
      <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
    </el-select>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
let emits = defineEmits(['update:modelValue'])
const props = defineProps({
  modelValue: {
    type: Object,
    default() {
      return {
        code1: '1',
        code2: '2'
      }
    }
  }
})
const localValue = reactive({ ...props.modelValue }); // 拷贝 modelValue
const options = ref([])
const change = () => {
  emits('update:modelValue', localValue)
}

onMounted(() => {
  console.log('值', props.modelValue)

  setTimeout(() => {
    options.value = [
      {
        value: 'Option1',
        label: 'Option1',
      },
      {
        value: 'Option2',
        label: 'Option2',
      },
      {
        value: 'Option3',
        label: 'Option3',
      },
    ]
  }, 10000)



})
</script>
<style scoped></style>
相关推荐
万少7 小时前
HarmonyOS 开发必会 5 种 Builder 详解
前端·harmonyos
橙序员小站9 小时前
Agent Skill 是什么?一文讲透 Agent Skill 的设计与实现
前端·后端
炫饭第一名11 小时前
速通Canvas指北🦮——基础入门篇
前端·javascript·程序员
王晓枫12 小时前
flutter接入三方库运行报错:Error running pod install
前端·flutter
符方昊12 小时前
React 19 对比 React 16 新特性解析
前端·react.js
ssshooter12 小时前
又被 Safari 差异坑了:textContent 拿到的值居然没换行?
前端
曲折12 小时前
Cesium-气象要素PNG色斑图叠加
前端·cesium
Forever7_12 小时前
Electron 淘汰!新的桌面端框架 更强大、更轻量化
前端·vue.js
不会敲代码112 小时前
前端组件化样式隔离实战:React CSS Modules、styled-components 与 Vue scoped 对比
css·vue.js·react.js
Angelial12 小时前
Vue3 嵌套路由 KeepAlive:动态缓存与反向配置方案
前端·vue.js