欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3.0 子組件如何修改父組件傳遞過來的值

 更新時間:2022年04月29日 10:18:42   作者:昵稱老重復(fù)  
這篇文章主要介紹了vue3.0 子組件如何修改父組件傳遞過來的值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

子組件修改父組件傳遞過來的值

vue 的子組件 不是 不能直接更改父組件的值,需要將props 的值 給到子組件,后再修改,

否則:Unexpected mutation of “props” prop.

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

toRefs 非常有用,這樣消費(fèi)組件就可以在不丟失響應(yīng)性的情況下對返回的對象進(jìn)行解構(gòu)/展開

使用

const { foo, bar } = reactive({
? ? foo: 1,
? ? bar: 2
})
// 核心解決, 使用reactive接收不會響應(yīng)時更新,需要使用toRefs
const props = defineProps({
? ? drawerStatus: {
? ? ? ? type: Boolean
? ? }
})
const {drawerStatus} = toRefs(props)

使用toRefs進(jìn)行解決

<template>
? ? <el-drawer v-model="drawerStatus" title="設(shè)置部門助理" :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)
// 新增角色數(shù)據(jù)
const formData = reactive({
})
const onSubmit = () => {
? ? handleClose()
}
const handleClose = () => {
? ? console.log('關(guān)閉抽屜')
? ? emits('upDrawerStatus', false)
}
</script>

子組件向父組件傳值emit的使用注意事項(xiàng)

子組件的寫法

需要從setup函數(shù)中引出{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">子組件觸發(fā)父組件的方法</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",'傳遞的數(shù)據(jù)')
? ? }
? ? 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);
? // 監(jiān)聽指定基礎(chǔ)類型數(shù)據(jù)
? ? const addNum=()=>{
? ? ? // ?name.value=Number(name.value) +1
? ? ? name.value++
? ? }
? ? watch(name, (now, prev) => {
? ? ? ? ? console.log(now, prev, 'count')
? ? })
? ? // 監(jiān)聽reactive創(chuàng)建的響應(yīng)式變量,可以直接監(jiān)聽對象,必須使用內(nèi)聯(lián)函數(shù)
? ? 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);//報(bào)錯
? }
})
</script>
<style>
</style>

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue.js創(chuàng)建Calendar日歷效果

    Vue.js創(chuàng)建Calendar日歷效果

    這篇文章主要為大家詳細(xì)介紹了Vue.js創(chuàng)建Calendar日歷效果的過程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • vue在線動態(tài)切換主題色方案

    vue在線動態(tài)切換主題色方案

    這篇文章主要介紹了vue在線動態(tài)切換主題色方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • vue中的router-view父子組件傳參方式

    vue中的router-view父子組件傳參方式

    這篇文章主要介紹了vue中的router-view父子組件傳參方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Vue3中的shallowRef?和shallowReactive對比分析

    Vue3中的shallowRef?和shallowReactive對比分析

    這篇文章主要介紹了Vue3中的shallowRef?和shallowReactive,通過示例代碼逐一對他們的使用做的詳細(xì)介紹,文末補(bǔ)充介紹了vue3的shallowRef()、shallowReactive()和shallowReadonly()的使用,需要的朋友可以參考下
    2023-01-01
  • 解析vue data不可以使用箭頭函數(shù)問題

    解析vue data不可以使用箭頭函數(shù)問題

    這篇文章主要介紹了vue data不可以使用箭頭函數(shù)問題,本文通過源碼解析給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • 詳解Vue 的異常處理機(jī)制

    詳解Vue 的異常處理機(jī)制

    這篇文章主要介紹了Vue 的異常處理機(jī)制,幫助大家更好的理解和學(xué)習(xí)vue框架,感興趣的朋友可以了解下
    2020-11-11
  • Vue2.0中三種常用傳值方式(父傳子、子傳父、非父子組件傳值)

    Vue2.0中三種常用傳值方式(父傳子、子傳父、非父子組件傳值)

    在Vue的框架開發(fā)的項(xiàng)目過程中,經(jīng)常會用到組件來管理不同的功能,有一些公共的組件會被提取出來。下面通過本文給大家介紹Vue開發(fā)中常用的三種傳值方式父傳子、子傳父、非父子組件傳值,需要的朋友參考下吧
    2018-08-08
  • 從vue源碼解析Vue.set()和this.$set()

    從vue源碼解析Vue.set()和this.$set()

    這篇文章主要介紹了從vue源碼看Vue.set()和this.$set()的相關(guān)知識,我們先來從Vue提供的Vue.set()和this.$set()這兩個api看看它內(nèi)部是怎么實(shí)現(xiàn)的。感興趣的朋友跟隨小編一起看看吧
    2018-08-08
  • vue3中v-model的用法詳解

    vue3中v-model的用法詳解

    vue 提供了很多自定義指令,大大方便了我們的開發(fā),其中最常用的也就是 v-model,他可以在組件上使用以實(shí)現(xiàn)雙向綁定。它可以綁定多種數(shù)據(jù)結(jié)構(gòu), 今天總結(jié)一下用法
    2023-04-04
  • vue項(xiàng)目中頁面底部出現(xiàn)白邊及空白區(qū)域錯誤的問題

    vue項(xiàng)目中頁面底部出現(xiàn)白邊及空白區(qū)域錯誤的問題

    這篇文章主要介紹了vue項(xiàng)目中頁面底部出現(xiàn)白邊及空白區(qū)域錯誤的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評論