vue2中如何自定義組件的v-model
如何自定義組件的v-model
工作中經(jīng)常會涉及到父子組件數(shù)據(jù)更新和頁面相應數(shù)據(jù)樣式改變的問題。
通俗點來說,就是涉及到公共組件的可以抽離出來作為子組件,如果父組件中與子組件相關的數(shù)據(jù)改變,子組件中的數(shù)據(jù)也會改變,如果子組件的數(shù)據(jù)改變,則需要做一定的處理才能改變父組件中的相關數(shù)據(jù)。
處理方式:考慮使用 自定義組件的v-model(vue2 中方式)
父組件
<search-tab :list="list" v-model="active" /> ... export default { ?? ?data(){ ?? ??? ?return { ?? ??? ??? ?list: [], ?? ??? ??? ?active: 0 ?? ??? ?} ?? ?} } ...
子組件 SearchTab
<el-tabs :value="active" @tab-click="handleClick"> ? <el-tab-pane v-for="(item, key) in list" :key="key" :label="item.name" :name="item.value"></el-tab-pane> </el-tabs>
... export default { ? name: 'SearchTab', ? props: { ? ? active: { ? ? ? type: Number, ? ? ? default: 0 ? ? }, ? ? list: Array ? }, ? model: { ? ? prop: 'active', ? ? event: 'changeActive' ? }, ? methods: { ? ? handleClick(val) { ? ? ? this.$emit('changeActive', val.name) ? ? } ? } } ...
這種方式如果涉及到選中組件的選中樣式問題,能夠響應式
而如果我們不用自定義組件的 v-model 方法,而是普通的父子組件通信,子組件改變父組件的值通過子組件中聲明的另一個來作為中間變量,監(jiān)聽中間變量的值來改變父組件中的值,這種方式雖然能改變值,但是選中樣式卻不能及時更新。
子組件示例
<el-tabs :value="active" @tab-click="handleClick"> ? <el-tab-pane v-for="(item, key) in list" :key="key" :label="item.name" :name="item.value"></el-tab-pane> </el-tabs>
... export default { ? name: 'SearchTab', ? props: { ? ? name: String, ? ? activeTab: { ? ? ? type: String, ? ? ? default: '0' ? ? }, ? ? list: Array ? }, ? data() { ? ? return { ? ? ? active: this.activeTab ? ? } ? }, ? watch: { ? ? active() { ? ? ? this.activeTab = this.active ? ? } ? }, ? methods: { ? ? handleClick(val) { ? ? ? console.log(val) ? ? } ? } } ...
vue2與vue3在自定義組件v-model的區(qū)別
在vue開發(fā)中,通常會對一個自定義的組件進行封裝,并實現(xiàn)v-model雙向綁定功能
在vue2中,通常這樣實現(xiàn)
父組件
<template> ? <Child v-model="number"></Child> ? ? </template>
<script> ? export default { ? ? data() { ? ? ? return { ? ? ? ? number: 0 ? ? ? } ? ? }, ? ? components: { ? ? ? Child: () => import("./Child.vue") ? ? } ? } </script>
子組件
<template> ? <button @click="handleClick">{{ value }}</button> </template>
<script> ? export default { ? ? props: { ? ? ? value: Number ? ? }, ? ? methods: { ? ? ? handleClick() { ? ? ? ? // 通過emit一個input事件出去,實現(xiàn) v-model ? ? ? ? this.$emit('input', this.value + 1) ? ? ? } ? ? } ? } </script>
在vue3中,通過這樣實現(xiàn)
父組件
<template> ? <Child v-model="number"></Child> ? ? </template>
<script lang="ts"> import { defineComponent, ref } from 'vue'; export default defineComponent({ ? setup() { ? ? const number = ref(0); ? ? return { ? ? ? number ? ? }; ? }, }); </script>
子組件
<template> ? <button @click="handleClick">{{ value }}</button> </template>
<script lang="ts"> import { defineComponent } from 'vue'; export default defineComponent({ ? props: { ? ? // 更換成了 modelValue ? ? modelValue: Number ? }, ? setup(props, { emit }) { ? ? // 關閉彈出層 ? ? const handleClick = () => emit('update:modelValue', props.modelValue + 1); ? ? return { handleClick }; ? }, }); </script>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 在Vue2中v-model和.sync區(qū)別解析
- vue2和vue3組件v-model區(qū)別詳析
- Vue2子組件綁定 v-model,實現(xiàn)父子組件通信方式
- Vue v-model相關知識總結
- vue2 v-model/v-text 中使用過濾器的方法示例
- vue 2.0組件與v-model詳解
- Vue2.0利用 v-model 實現(xiàn)組件props雙向綁定的優(yōu)美解決方案
- vue2 如何實現(xiàn)div contenteditable=“true”(類似于v-model)的效果
- vue2與vue3雙向數(shù)據(jù)綁定的區(qū)別說明
- vue 2 實現(xiàn)自定義組件一到多個v-model雙向數(shù)據(jù)綁定的方法(最新推薦)
相關文章
淺析Proxy可以優(yōu)化vue的數(shù)據(jù)監(jiān)聽機制問題及實現(xiàn)思路
這篇文章主要介紹了淺析Proxy可以優(yōu)化vue的數(shù)據(jù)監(jiān)聽機制問題及實現(xiàn)思路,需要的朋友可以參考下2018-11-11詳解Vue-cli webpack移動端自動化構建rem問題
這篇文章主要介紹了詳解Vue-cli webpack移動端自動化構建rem問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04vue使用require.context實現(xiàn)動態(tài)注冊路由
這篇文章主要介紹了vue使用require.context實現(xiàn)動態(tài)注冊路由的方法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12vue不用import直接調(diào)用實現(xiàn)接口api文件封裝
這篇文章主要為大家介紹了vue不用import直接調(diào)用實現(xiàn)接口api文件封裝,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06vuex 解決報錯this.$store.commit is not a function的方法
這篇文章主要介紹了vuex 解決報錯this.$store.commit is not a function的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12vue-router beforeEach跳轉路由驗證用戶登錄狀態(tài)
這篇文章主要介紹了vue-router beforeEach跳轉路由驗證用戶登錄狀態(tài),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12