大飞_dafei

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Vue3 在 Element-plus中 v-model, update:modelValue 线上环境报错

Vue3 在 Element-plus中 v-model, update:modelValue 线上环境报错 modelValue is not defined,在本地开发环境没有问题

01)错误场景复现

复制代码
<template>
  <!-- 父组件-->
  <div class="fei-parent">
    <p>
      <button @click="visible=true"> 打开弹窗 </button>
    </p>

    <p>
      <Bar v-model="visible"/>
    </p>

  </div>
</template>

<script setup>
  import {ref} from "vue";
  import Bar from './bar.vue'
  const visible = ref(false);
</script>
------------------------------------------------------
<template>
  <el-dialog title="新增-修改" :width="560" v-model="modelValue" :before-close="onClose">
    <div>
      aaaaaaaaaaa__报错了
    </div>
  </el-dialog>

</template>

<script setup>
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  }
})

const emits = defineEmits(['update:modelValue'])

const onClose = () => {
  emits('update:modelValue', false)
}

</script>
复制代码

 

 

02)解决方案01:

在父组件上使用 v-if

<Bar v-if="visible" v-model="visible"/>

03)解决方案02:

在子组件中使用computed,重新定义值

复制代码
<template>
  <el-dialog title="新增-修改" :width="560" v-model="editVisible" :before-close="onClose">
    <div>
      aaaaaaaaaaa__ok_大飞
    </div>
  </el-dialog>
</template>

<script setup>
import {computed} from "vue";

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  }
})

const emits = defineEmits(['update:modelValue'])

const editVisible = computed({ // 重新定义
  get: () => props.modelValue,
  set: (value) => emits("update:modelValue", value),
})

const onClose = () => {
  emits('update:modelValue', false)
}

</script>
复制代码

 

posted on   大飞_dafei  阅读(3180)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示