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

vue3?封裝自定義組件v-model的示例

 更新時間:2022年07月27日 09:48:25   作者:當代詞圣李白  
這篇文章主要介紹了vue3?封裝自定義組件v-model及自定義組件使用v-model,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

首先要注意 vue3中 v-model 默認綁定的變量名變了,從原理的 value 改成了 modelValue,
如果要改變變量的值,要執(zhí)行一個事件 this.$emit("update:modelValue", value);

在這里插入圖片描述

在這里插入圖片描述

<template>
	<div class="inline">
		<input :type="password ? 'password' : 'text'" ref="input" @input="handleInput" @blur="handleBlur($event.target.value)" :placeholder="placeholder" />
	</div>
</template>
<script>
export default {
	name: "dg-Input",
	props: {
		type: {
			type: String,
			requided: true,
		},
		placeholder: {
			type: String,
		},
		password: {
			default: false,
		},
		modelValue: [String, Number],
	},
	data() {
		return {};
	},
	computed: {
		nativeInputValue() {
			return this.modelValue === null || this.modelValue === undefined ? "" : String(this.modelValue);
		},
	},
	methods: {
		handleInput(event) {
			let value = event.target.value;
			this.$emit("update:modelValue", value);
			this.$emit("input", value);
			this.$nextTick(this.setNativeInputValue);
		},
		setNativeInputValue() {
			const input = this.$refs.input;
			if (input.value === this.nativeInputValue) return;
			input.value = this.nativeInputValue;
		},

	
	},
	mounted() {
		this.setNativeInputValue();
	},
};
</script>
<style lang="scss" scoped>
.inline {
	display: inline-block;
	input {
		width: 100%;
		height: 100%;
		padding-left: 5px;
	}
}
</style>

vue3文檔地址

補充:下面看下vue3中自定義組件使用v-model

vue2 中的v-model

v-model本質(zhì)上是一個語法糖,如下代碼

<input v-model="test">
<!--上面代碼本質(zhì)上就是下方代碼-->
<input :value="test" @input="test = $event.target.value">

因此,對于一個帶有 v-model 的組件(核心用法),它應該如下:

帶有v-model的父組件通過綁定的value值(即v-model的綁定值)傳給子組件,子組件通過 prop接收一個 value;
子組件利用oninput事件實時通過 $emit 觸發(fā)父組件input 事件,并傳入新值value給父組件;

父組件

<template>
    <div>
        <child v-model="msg" @input="msg = $event.target.value" />
        <!--<child :value="msg" @input="msg = $event.target.value" />-->
    </div>
</template>
<script>
import child from './components/Child.vue'
export default {
    components: {
        child
    },
    data() {
        return {
            msg: ''
        }
    }
}
</script>

子組件child

<template>
    <input type="text" :value="modelValue" @input="handleInput">
</template>
<script>
export default {
    name: 'Child',
    props:['value'],
    data() {
        return {
            modelValue: this.value
        }
    },
    methods: {
        handleInput(event) {
            this.$emit('input', event)
        }
    }
}

vue3中的 v-model

vue3中的v-model默認綁定的不是value,而是modelValue,接收的方法由input改為@update:modelValue。

<template>
  <child v-model="msg" />
  <p>{{msg}}</p>
</template>
 
<script lang="ts">
import { defineComponent,ref} from 'vue';
import child from './components/child/index.vue'
 
export default defineComponent({
  name: 'App',
  components:{
    child
  },
  setup(){
    const msg = ref('1')
    return{
      msg
    }
  }
});
</script>
<template>
    <input type="text" :value="modelValue" @input="onInput">
</template>
<script lang="ts">
import {defineComponent} from 'vue'
export default defineComponent({
    name:'ChildInput',
    props:{
        modelValue:{
            type:String
        }
    },
    setup(props, context){
        const onInput = (e: Event) =>{
            context.emit('update:modelValue',e.target.value)
        }
        return{
            onInput
        }
    }
})
</script>

到此這篇關(guān)于vue3 封裝自定義組件v-model的文章就介紹到這了,更多相關(guān)vue3 自定義組件v-model內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • axios中cookie跨域及相關(guān)配置示例詳解

    axios中cookie跨域及相關(guān)配置示例詳解

    自從入了 Vue 之后,一直在用 axios 這個庫來做一些異步請求。下面這篇文章主要給大家介紹了關(guān)于axios中cookie跨域及相關(guān)配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起看看吧。
    2017-12-12
  • vue中eslint導致的報錯問題及解決

    vue中eslint導致的報錯問題及解決

    這篇文章主要介紹了vue中eslint導致的報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue實現(xiàn)全局組件自動注冊,無需再單獨引用

    vue實現(xiàn)全局組件自動注冊,無需再單獨引用

    這篇文章主要介紹了vue實現(xiàn)全局組件自動注冊,無需再單獨引用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Vue.js樣式動態(tài)綁定實現(xiàn)小結(jié)

    Vue.js樣式動態(tài)綁定實現(xiàn)小結(jié)

    這篇文章主要介紹了Vue.js樣式動態(tài)綁定實現(xiàn)小結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • vue中如何進行異步請求

    vue中如何進行異步請求

    前端的數(shù)據(jù)均是通過接口請求拿到的,而Vue本身不支持ajax請求,那么該怎么解決Vue中的異步請求呢?這兒提供了幾種方法,希望對大家有所幫助
    2022-05-05
  • 發(fā)布訂閱模式在vue中的實際運用實例詳解

    發(fā)布訂閱模式在vue中的實際運用實例詳解

    訂閱發(fā)布模式定義了一種一對多的依賴關(guān)系,讓多個訂閱者對象同時監(jiān)聽某一個主題對象。這篇文章主要介紹了發(fā)布訂閱模式在vue中的實際運用,需要的朋友可以參考下
    2019-06-06
  • Vue使用輪詢定時發(fā)送請求代碼

    Vue使用輪詢定時發(fā)送請求代碼

    這篇文章主要介紹了Vue使用輪詢定時發(fā)送請求代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • vue實現(xiàn)input輸入模糊查詢的三種方式

    vue實現(xiàn)input輸入模糊查詢的三種方式

    本文主要介紹了vue實現(xiàn)input輸入模糊查詢的三種方式嗎,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • Vue使用vm.$set()解決對象新增屬性不能響應的問題

    Vue使用vm.$set()解決對象新增屬性不能響應的問題

    這篇文章主要介紹了Vue使用vm.$set()解決對象新增屬性不能響應的問題,為了解決這個問題,Vue提供了一個特殊的方法vm.$set(object, propertyName, value),也可以使用全局的Vue.set(object, propertyName, value)方法,需要的朋友可以參考下
    2023-05-05
  • Element?Plus的el-tree-select組件懶加載+數(shù)據(jù)回顯詳解

    Element?Plus的el-tree-select組件懶加載+數(shù)據(jù)回顯詳解

    el-tree-select組件是el-tree和el-select的結(jié)合體,他們的原始屬性未被更改,下面這篇文章主要給大家介紹了關(guān)于Element?Plus的el-tree-select組件懶加載+數(shù)據(jù)回顯的相關(guān)資料,需要的朋友可以參考下
    2022-11-11

最新評論