当前位置: 首页 > 知识库问答 >
问题:

vue3父子组件交互问题,哪种写法更优,为什么?

邓深
2023-08-25

描述:在父组件引入子组件,子组件是一个弹窗,两种方式都可实现,哪种写法更好,为什么?
方式1是在父组件定义控制子组件是否打开的参数以及传递的参数,在子组件进行监听赋值
方式2是用ref操作子组件方法

方式一:

父组件<template>   <el-button     text      type="warning"      icon="CircleCheck"     @click="handleSetRole(scope.row)"   >分配角色</el-button>   <AssigningRoles      :is-open="isOpen"      :params-data="paramsData"      @get-role-result="getRoleResult">   </AssigningRoles></template><script setup>    const isOpen = ref(false) // 控制子组件是否显示    const paramsData = ref({}) // 其他数据传递    // 方法    const handleSetRole = () => {       isOpen.value = true       paramsData.value = {           account: row.account,           nickname: row.nickname        }     }     分配角色回调     const getRoleResult = (flag) => {         isOpen.value = false         flag && getList()      }</script>子组件<template> <el-dialog    title="分配角色"    v-model="isOpen"    width="500px"    append-to-body    :close-on-click-modal="false"    @close="cancel">    <el-form>        <el-input v-model="form.account"></el-input>        ....     </el-form>     <template #footer>      <el-button text @click="cancel">取消</el-button>      <el-button type="primary" @click="submitForm">确定</el-button>    </template> </el-dialog></template><script setup>    const { proxy } = getCurrentInstance()    const form = reactive({       account: '',       nickname: '',       role: []    })   // props    const props = defineProps({      isOpen: {        type: Boolean,        default: false      },      paramsData: {        type: Object,        default: () => ({})      }    })    const { isOpen } = toRefs(props)    // watch监听    watch(      () => props.paramsData,      (newVal, oldVal) => {        form.account = newVal.account        form.nickname = newVal.nickname        form.role = []      },      { immediate: true, deep: true }    )    // 子组件传父组件emit    const emit = defineEmits(['get-role-result'])    // 取消    const cancel = () => {      emit('get-role-result', false)    }    // 提交    const submitForm = () => {      assignUserRole(form.value).then((res) => {        proxy.$modal.msgSuccess('分配成功')        emit('get-role-result', true)      })    }</script>

方式二:

父组件<template>   <el-button     text      type="warning"      icon="CircleCheck"     @click="handleSetRole(scope.row)"   >分配角色</el-button>   <AssigningRoles      ref="editFormRef"      @getEditResult="editEvent">   </AssigningRoles></template><script setup>    const editFormRef = ref(null)    // 分配角色    function handleSetRole(row) {      proxy.$refs.editFormRef.openEditForm(row)    }    // 分配角色回调    function editEvent() {      getList()    }</script>子组件<template> <el-dialog    title="分配角色"    v-model="editVisible.visible"    width="500px"    append-to-body    :close-on-click-modal="false"    @close="cancel">    <el-form>        <el-input v-model="form.account"></el-input>        ....     </el-form>     <template #footer>      <el-button text @click="cancel">取消</el-button>      <el-button type="primary" @click="submitForm">确定</el-button>    </template> </el-dialog></template><script setup>    const { proxy } = getCurrentInstance()    // 定义form表单数据    const form = reactive({      account: '',      nickname: '',      role: []    })    // 编辑窗口参数    const editVisible = reactive({      visible: false // 窗口开关    })    const emit = defineEmits(['getEditResult'])    const openEditForm = (data) => {      form.account = data.account      form.nickname = data.nickname      form.role = []      editVisible.visible = true    }    // 取消    const cancel = () => {      editVisible.visible = false    }    // 提交    const submitForm = () => {      assignUserRole(form.value).then((res) => {        proxy.$modal.msgSuccess('分配成功')        editVisible.visible = false        emit('getEditResult')      })    }    defineExpose({ openEditForm })</script>

共有1个答案

吴胜
2023-08-25

一种是状态控制(响应式),一种是直接控制(命令式)

对于 Vue 来说,推荐使用响应式(本来 Vue 设计就是数据触发呈现变化的)

但是有些组件确实控制性要求较强,需要主动控制的,使用 ref 处理会方便一些。不过不建议使用 ref 来直接修改数据。

 类似资料:
  • 执行了click事件后,为什么videoUrl的值在页面上更新了,控制台里没更新啊 想知道原因

  • 第一种方式: App.vue NavTree.vue 第二种方式: App.vue NavTree.vue 上面这两个种 ids 和 nav_ids 传值, 哪种传值会更快,还是说都一样,如果 navs 很多的时候

  • 本文向大家介绍浅谈Vue父子组件和非父子组件传值问题,包括了浅谈Vue父子组件和非父子组件传值问题的使用技巧和注意事项,需要的朋友参考一下 本文介绍了浅谈Vue父子组件和非父子组件传值问题,分享给大家,具体如下: 1.如何创建组件 1.新建一个组件,如:在goods文件夹下新建goodsList.vue 2.在main.js中引入 import goodsList from 'goods/good

  • vue3中子组件向父组件传值 在传值的时候为什么只能在声明一个方法的时候传递,而不能在定义click的时候传递呢

  • 本文向大家介绍vuejs父子组件通信的问题,包括了vuejs父子组件通信的问题的使用技巧和注意事项,需要的朋友参考一下 父子组件之间可以通过props进行通信: 组件的定义: 1.创建component类:  2.注册一个tagnme: 局部注册: 模板注意事项:  因为 Vue 就是原生的DOM,所以有些自定义标签可能不符合DOM标准,比如想在 table 中自定义一个 tr,如果直接插入 my

  • 父组件 子组件 为什么执行getSonData的时候,无法获取到子组件的data?sonRef.value.data只能在onMounted内使用吗?不能在父组件的方法里执行?