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 來(lái)達(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開(kāi)發(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() {
? ? ? ? // 通過(guò)emit一個(gè)input事件出去,實(shí)現(xiàn) v-model
? ? ? ? this.$emit('input', this.value + 1)
? ? ? }
? ? }
? }
</script>在 vue 3 中,通過(guò)這樣實(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)文章
關(guān)于IDEA中的.VUE文件報(bào)錯(cuò) Export declarations are not supported by cu
這篇文章主要介紹了關(guān)于IDEA中的.VUE文件報(bào)錯(cuò) Export declarations are not supported by current JavaScript version的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
vue-router如何實(shí)現(xiàn)history模式配置
這篇文章主要介紹了vue-router如何實(shí)現(xiàn)history模式配置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
vue中實(shí)現(xiàn)全屏以及對(duì)退出全屏的監(jiān)聽(tīng)
本文主要介紹了vue中實(shí)現(xiàn)全屏以及對(duì)退出全屏的監(jiān)聽(tīng),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
vue手寫<RouterLink/>組件實(shí)現(xiàn)demo詳解
這篇文章主要為大家介紹了vue手寫<RouterLink/>組件實(shí)現(xiàn)demo詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Vue Element前端應(yīng)用開(kāi)發(fā)之樹(shù)列表組件
本篇隨筆主要介紹樹(shù)列表組件和下拉列表樹(shù)組件在項(xiàng)目中的使用,以及一個(gè)SplitPanel的組件。2021-05-05

