vue如何自定義組件v-model
vue自定義組件v-model
官網(wǎng)的話
允許一個(gè)自定義組件在使用 v-model 時(shí)定制 prop 和 event。
默認(rèn)情況下,一個(gè)組件上的 v-model 會(huì)把 value 用作 prop 且把 input 用作 event,但是一些輸入類型比如單選框和復(fù)選框按鈕可能想使用 value prop 來達(dá)到不同的目的。
使用 model 選項(xiàng)可以回避這些情況產(chǎn)生的沖突。
效果

父組件:
<template>
<view class="index">
我是父級(jí)組件{{msg}}
<!-- 相當(dāng)于: <node :value="msg" @input="v=>msg=v"></node>-->
<node v-model="msg"></node>
</view>
</template>
<script lang="ts">
import Vue from "vue";
import node from"./node.vue"
export default Vue.extend({
components:{
node
},
data() {
return {
msg:"abc",
};
},
mounted() {
// uni.navigateTo({
// url: "/pages/login/login",
// });
},
});
</script>
<style scoped>
.index{
border: solid 1px orange;
}
</style>子組件
<template>
<view class="node">
<text>我是子級(jí)組件</text>
<el-input v-model="result" placeholder="請(qǐng)輸入內(nèi)容"></el-input>
</view>
</template>
<script lang="ts">
import Vue from "vue";
export default Vue.extend({
props: {
value: {
type: [String, Number],
},
},
data() {
return {};
},
computed: {
result: {
get(): string | number {
return this.value;
},
set(v: string | number) {
console.log("我執(zhí)行了");
this.$emit("input", v);
},
},
},
});
</script>
<style scoped>
.node {
margin: 50px 0;
width: 500px;
border: solid black 2px;
}
</style>如果要想自定義value的名字和事件名,修改一下字組件即可,如:
<template>
<view class="node">
<text>我是子級(jí)組件</text>
<el-input v-model="result" placeholder="請(qǐng)輸入內(nèi)容"></el-input>
</view>
</template>
<script lang="ts">
import Vue from "vue";
export default Vue.extend({
model: {
prop: 'modelValue',
event: 'change'
},
props: {
modelValue: {
type: [String, Number],
},
},
data() {
return {};
},
computed: {
result: {
get(): string | number {
return this.modelValue;
},
set(v: string | number) {
console.log("我執(zhí)行了");
this.$emit("change", v);
},
},
},
});
</script>
<style scoped>
.node {
margin: 50px 0;
width: 500px;
border: solid black 2px;
}
</style>vue2與vue3在自定義組件v-model上的區(qū)別
在vue開發(fā)中,通常會(huì)對(duì)一個(gè)自定義的組件進(jìn)行封裝,并實(shí)現(xiàn)v-model雙向綁定功能
在 Vue 2 中,通常這樣實(shí)現(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一個(gè)input事件出去,實(shí)現(xiàn) v-model
? ? ? ? this.$emit('input', this.value + 1)
? ? ? }
? ? }
? }
</script>在 vue 3 中,通過這樣實(shí)現(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 }) {
? ? // 關(guān)閉彈出層
? ? const handleClick = () => emit('update:modelValue', props.modelValue + 1);
? ? return { handleClick };
? },
});
</script>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue插件draggable實(shí)現(xiàn)拖拽移動(dòng)圖片順序
這篇文章主要為大家詳細(xì)介紹了vue插件draggable實(shí)現(xiàn)拖拽移動(dòng)圖片順序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Vue實(shí)現(xiàn)雙向綁定的原理以及響應(yīng)式數(shù)據(jù)的方法
這篇文章主要介紹了Vue實(shí)現(xiàn)雙向綁定的原理以及響應(yīng)式數(shù)據(jù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
vue2中組件互相調(diào)用實(shí)例methods中的方法實(shí)現(xiàn)詳解
vue在同一個(gè)組件內(nèi),方法之間經(jīng)常需要互相調(diào)用,下面這篇文章主要給大家介紹了關(guān)于vue2中組件互相調(diào)用實(shí)例methods中的方法實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
vue 中directive功能的簡單實(shí)現(xiàn)
本篇介紹directive的簡單實(shí)現(xiàn),主要學(xué)習(xí)其實(shí)現(xiàn)的思路及代碼的設(shè)計(jì),需要的朋友參考下吧2018-01-01
關(guān)于vuex的學(xué)習(xí)實(shí)踐筆記
vuex是vue的狀態(tài)管理模式,主要可以解決父子組件嵌套層數(shù)較多,或者兄弟組件之間需要維護(hù)同一個(gè)狀態(tài)的情況。下面這篇文章主要給大家介紹了關(guān)于學(xué)習(xí)vuex的相關(guān)資料,需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。2017-04-04
vue如何解決watch和methods值執(zhí)行順序問題
這篇文章主要介紹了vue如何解決watch和methods值執(zhí)行順序問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

