vue3中update:modelValue的使用與不生效問題解決
v-model中update:modelValue
v-model的主要原因是由于value和input事件可能另有它用,那么我們可不可以直接使用另外的屬性和方法,而不需要去通過model進(jìn)行定義。
vue3中就實(shí)現(xiàn)了這個(gè)功能,v-model綁定的不再是value,而是modelValue,接收的方法也不再是input,而是update:modelValue
寫法:
<ChildComponent v-model:title="title" /> //或者 <ChildComponent :modelValue = "title" @update:modelValue = "title = $event"> // 或者 <ChildComponent :title="title" @update:title = "title = $event" />
使用:
父組件
<wm-tinymce ref="editor" v-model="data.introduction" :min-height="400" name="職能管理" placeholder="請(qǐng)編輯職能大類" />
子組件
<div class="tinymce-container">
<editor
v-model="tinymceData"
:api-key="key"
:init="tinymceOptions"
:name="name"
:readonly="tinymceReadOnly"
/>
</div>
<script>
import { ref, watch, watchEffect } from 'vue'
import Editor from '@tinymce/tinymce-vue'
import { key, plugins, toolbar, setting } from './config'
export default {
name: 'WmTinymce',
components: { Editor },
props: {
modelValue: {
type: String,
default: '',
},
readOnly: {
type: Boolean,
default: false,
},
options: {
type: Object,
default() {
return { plugins, toolbar }
},
},
name: {
type: String,
default: '',
},
},
emits: ['update:modelValue'],
setup(props, { emit }) {
const tinymceData = ref(props.modelValue) // 編輯器數(shù)據(jù)
watch(
() => tinymceData.value,
(data) => emit('update:modelValue', data)
) // 監(jiān)聽富文本輸入值變動(dòng)
return {
tinymceData,
}
},
}
</script>
vue3子組件update:modelValue不生效問題
用event去做綁定
<ChildComponent :title=“title” @update:title = “title = $event” />
也就是加上 @update:title = “title = $event” 測(cè)試成功
總結(jié)
到此這篇關(guān)于vue3中update:modelValue的使用與不生效問題解決的文章就介紹到這了,更多相關(guān)vue3 update:modelValue使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue項(xiàng)目組件化工程開發(fā)實(shí)踐方案
這篇文章主要介紹了Vue項(xiàng)目組件化工程開發(fā)實(shí)踐方案,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01
Vue前端實(shí)現(xiàn)導(dǎo)出頁面為word的兩種方法
這篇文章主要介紹了Vue前端實(shí)現(xiàn)導(dǎo)出頁面為word的兩種方法,文中通過代碼示例和圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-12-12
Vue3數(shù)據(jù)更新,頁面沒有同步更新的問題及解決
這篇文章主要介紹了Vue3數(shù)據(jù)更新,頁面沒有同步更新的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vant 解決tab切換插件標(biāo)題樣式自定義的問題
這篇文章主要介紹了vant 解決tab切換插件標(biāo)題樣式自定義的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue 讀取HTMLCollection列表的length為0問題
這篇文章主要介紹了Vue 讀取HTMLCollection列表的length為0問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

