vue3.0 子组件如何修改父组件传递过来的值

 更新时间:2022年04月29日 10:18:42   作者:昵称老重复  
这篇文章主要介绍了vue3.0 子组件如何修改父组件传递过来的值,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

子组件修改父组件传递过来的值

vue 的子组件 不是 不能直接更改父组件的值,需要将props 的值 给到子组件,后再修改,

否则:Unexpected mutation of “props” prop.

vue3提供了一个解决:toRefs:https://v3.cn.vuejs.org/api/refs-api.html#torefs

toRefs 非常有用,这样消费组件就可以在不丢失响应性的情况下对返回的对象进行解构/展开

使用

const { foo, bar } = reactive({
    foo: 1,
    bar: 2
})
// 核心解决, 使用reactive接收不会响应时更新,需要使用toRefs
const props = defineProps({
    drawerStatus: {
        type: Boolean
    }
})
const {drawerStatus} = toRefs(props)

使用toRefs进行解决

<template>
    <el-drawer v-model="drawerStatus" title="设置部门助理" :before-close="handleClose">
        <div class="drawer-footer">
            <el-button>取消</el-button>
            <el-button type="primary" @click="onSubmit">确定</el-button>
        </div>
    </el-drawer>
    
</template>
<script setup>
import {reactive, toRefs} from 'vue'
const props = defineProps({
    drawerStatus: {
        type: Boolean
    }
})
const emits = defineEmits(['upDrawerStatus'])
const {drawerStatus} = toRefs(props)
console.log(33333333, drawerStatus)
// 新增角色数据
const formData = reactive({
})
const onSubmit = () => {
    handleClose()
}
const handleClose = () => {
    console.log('关闭抽屉')
    emits('upDrawerStatus', false)
}
</script>

子组件向父组件传值emit的使用注意事项

子组件的写法

需要从setup函数中引出{emit}

<template>
  <div id="center" v-if="isShow">
    <h2><slot>my model</slot></h2>
    <el-button type="primary" @click="btnclose">传递事件</el-button>
    <el-button type="primary" @click="btnparents">子组件触发父组件的方法</el-button>
  </div>
</template>
<script lang="ts">
import {
  defineComponent,getCurrentInstance
} from "vue";
export default defineComponent({
  props: {
    isShow:{
      type:Boolean,
      default:true
    }
  },
  emits: {
    "my-close": null,
  },
  setup(props, {emit}) {
    const {proxy}:any=getCurrentInstance()
    const btnclose = () => {
      emit("my-close",'传递的数据')
    }
    const btnparents=()=>{
      console.log(proxy);
      proxy.$parent.addNum()
    }
    return {
      btnclose,
      proxy,
      btnparents
    };
  },
});
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

父组件使用

<template>
  <HelloWorld @my-close="myModealHide" :isShow="myModalShow">solt</HelloWorld>
  <el-button @click="myModalBlock">my modal2</el-button>
  <el-button type="primary" @click="toDelit">用户详情</el-button>
</template>
<script lang="ts">
import { defineComponent, computed, onMounted, watch,ref,getCurrentInstance,reactive,nextTick ,toRefs} from 'vue'
import {
  useStore,
  mapState,
} from 'vuex'
import {useRoute,useRouter,useLink,UseLinkOptions} from "vue-router"
import { useI18n } from "vue-i18n";
import {data} from "@/utils/TypeState"
import HelloWorld from '@/components/HelloWorld.vue'
export default defineComponent({
  components:{
    HelloWorld
  },
  setup(props, context){
    console.log('context',context);
    const store=useStore()
    const {proxy}:any=getCurrentInstance()
    const number=ref<string|Number>(store.state.app.age)
    const Route=useRoute()
    const RouteLink=useLink
    const { t } = useI18n();
    const languageD=()=>{
      proxy.$i18n.locale=data.seleLanguage
    }
    console.log(store.state.app.allMenuList instanceof  Array);
    console.log(proxy);
  // 监听指定基础类型数据
    const addNum=()=>{
      //  name.value=Number(name.value) +1
      name.value++
    }
    watch(name, (now, prev) => {
          console.log(now, prev, 'count')
    })
    // 监听reactive创建的响应式变量,可以直接监听对象,必须使用内联函数
    watch(() => data.tableData, (now, prev) => {
      console.log(now, prev, 'tableData')
    })
    const myModalShow = ref(false)
    const myModealHide=(val:any)=>{
      myModalShow.value=false
      console.log(val);
    }
    const myModalBlock =()=>{
      myModalShow.value=true
    }
    const toDelit=():void=>{
      proxy.$router.push("/userAdminDetils")
    }
    return {
      age,
      number,
      proxy,
      store,
      name,
      addNum,
      ...toRefs(data) ,
      t,
      languageD,
      myModealHide,
      myModalBlock,
      myModalShow,
      toDelit
    }
  },
  created () {
    console.log(this.$route);
    console.log(this.store.state.app.age);
    // console.log(this.$store);//报错
  }
})
</script>
<style>
</style>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vuex的store中的Module用法及说明

    Vuex的store中的Module用法及说明

    这篇文章主要介绍了Vuex的store中的Module用法及说明,具有很好的参考价值,希望对大家有所帮助。
    2023-01-01
  • vue css 引入asstes中的图片无法显示的四种解决方法

    vue css 引入asstes中的图片无法显示的四种解决方法

    这篇文章主要介绍了vue css 引入asstes中的图片 无法显示的几种解决方案,本文给出了四种解决方法,每种方法给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03
  • 一文带你搞懂Vue中Provide/Inject的使用与高级应用

    一文带你搞懂Vue中Provide/Inject的使用与高级应用

    这篇文章将详细介绍如何在 Vue.js 中使用 provide 和 inject 模式,并探讨其在实际应用中的高级用法,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-11-11
  • vue中实现点击变成全屏的多种方法

    vue中实现点击变成全屏的多种方法

    这篇文章主要介绍了vue中实现点击变成全屏的多种方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • 详解Vue Cli浏览器兼容性实践

    详解Vue Cli浏览器兼容性实践

    这篇文章主要介绍了详解Vue Cli浏览器兼容性实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • vue中的watch监听数据变化及watch中各属性的详解

    vue中的watch监听数据变化及watch中各属性的详解

    这篇文章主要介绍了vue中的watch监听数据变化及watch中的immediate、handler和deep属性详解,本文大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友参考下吧
    2018-09-09
  • vue2.0+elementui实现一个上门取件时间组件

    vue2.0+elementui实现一个上门取件时间组件

    这篇文章主要给大家介绍了关于vue2.0+elementui实现一个上门取件时间组件的相关资料,用于预约上门服务时间 看到网上有很多乱七八糟的代码,看着头疼,于是自己写了一个简单的,需要的朋友可以参考下
    2024-02-02
  • vue如何设置描点跳转到对应页面

    vue如何设置描点跳转到对应页面

    这篇文章主要介绍了vue如何设置描点跳转到对应页面问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • vue下载二进制流图片操作

    vue下载二进制流图片操作

    这篇文章主要介绍了vue下载二进制流图片操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • Vue项目分环境打包的实现步骤

    Vue项目分环境打包的实现步骤

    这篇文章主要介绍了Vue项目如何分环境打包,实现方法大概分为六步骤,需要的朋友可以参考下
    2018-04-04

最新评论