vue3中update:modelValue的使用與不生效問題解決
v-model中update:modelValue
v-model的主要原因是由于value和input事件可能另有它用,那么我們可不可以直接使用另外的屬性和方法,而不需要去通過model進(jìn)行定義。
vue3中就實(shí)現(xiàn)了這個功能,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="請編輯職能大類" />
子組件
<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)聽富文本輸入值變動 return { tinymceData, } }, } </script>
vue3子組件update:modelValue不生效問題
用event去做綁定
<ChildComponent :title=“title” @update:title = “title = $event” />
也就是加上 @update:title = “title = $event”
測試成功
總結(jié)
到此這篇關(guān)于vue3中update:modelValue的使用與不生效問題解決的文章就介紹到這了,更多相關(guān)vue3 update:modelValue使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用iview的modal彈窗嵌套modal出現(xiàn)格式錯誤的解決
這篇文章主要介紹了vue使用iview的modal彈窗嵌套modal出現(xiàn)格式錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09詳解Vue中Computed與watch的用法與區(qū)別
這篇文章主要介紹了Vue中computed和watch的使用與區(qū)別,文中通過示例為大家進(jìn)行了詳細(xì)講解,對Vue感興趣的同學(xué),可以學(xué)習(xí)一下2022-04-04vue如何將base64流數(shù)據(jù)轉(zhuǎn)成pdf文件并在新頁面打開
這篇文章主要介紹了vue如何將base64流數(shù)據(jù)轉(zhuǎn)成pdf文件并在新頁面打開問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02Vue全局使用less樣式,組件使用全局樣式文件中定義的變量操作
這篇文章主要介紹了Vue全局使用less樣式,組件使用全局樣式文件中定義的變量操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Vue3?Radio單選切換展示不同內(nèi)容實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue3?Radio單選切換展示不同內(nèi)容,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07Vite版本更新檢查實(shí)現(xiàn)頁面自動刷新的解決思路
這篇文章主要給大家介紹了關(guān)于Vite版本更新檢查實(shí)現(xiàn)頁面自動刷新的解決思路,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-02-02