>[success] # v-model
~~~
1.'非兼容':用于自定義組件時,v-model prop 和事件默認名稱已更改:
1.1.'prop':value -> modelValue;
1.2.'event':input -> update:modelValue;
2.'非兼容':v-bind 的 .sync 修飾符和組件的 model 選項已移除,可用 v-model 作為代替;
3.'新增':現在可以在同一個組件上使用多個 v-model 進行雙向綁定;
4.'新增':現在可以自定義 v-model 修飾符。
~~~
>[info] ## v-model 非組件基礎使用
~~~
1.'v-model' 指令在表單 '<input>、<textarea> 及 <select>' 元素上創建雙向數據綁定,但 v-model 本質是語法糖
<input v-model="searchText" /> 這種寫法等同于
<input :value="searchText" @input="searchText = $event.target.value" />
~~~
>[info] ## Vue2.x
~~~
1.'v-model' 是語法糖,因此在組件上使用也是同樣的,在組件父子值傳遞往往需要在使用的時候定義事件和
props, '<ChildComponent :value="pageTitle" @input="pageTitle = $event" />'
可以簡寫 '<ChildComponent v-model="pageTitle" />'
當然Vue 也提供了屬性'model' 可以自定義屬性不再限制于'value 和 input'
// ChildComponent.vue
export default {
model: {
prop: 'title',
event: 'change'
},
props: {
// 這將允許 `value` 屬性用于其他用途
value: String,
// 使用 `title` 代替 `value` 作為 model 的 prop
title: {
type: String,
default: 'Default title'
}
}
}
但是v-model 最大的問題只能作用一次及不會出現以下情況
'<ChildComponent v-model="pageTitle" v-model="pageContent"/>'
這時候出現了'.sync' 修飾符來解決這類問題'<ChildComponent :title.sync="pageTitle" />' 可以綁定多個
目的就是一個語法糖的省略用法,更多的詳細案例'http://www.hmoore.net/cyyspring/vuejs/1108931'
~~~
>[info] ## 3.x
~~~
1.來看'3.x','v-model'在'2.x' 的弊端'v-model' 只能在組件使用一次,并且如果想實現類似效果使用多次需要
'.sync' 來定義使用這個語法糖,本質上'v-model 和sync'在組建上做了類似的語法功能,在'3.x'將'.sync'
移除,為了讓組件可以使用多個'v-model' 也從原來'value 和input'變成了'modelValue 和 update:modelValue'
~~~
>[danger] ##### 簡版的先消化理解
~~~js
1.在組件使用'<ChildComponent v-model="pageTitle" />' 是下面的簡寫
<custom-input
:modelValue="searchText"
@update:modelValue="searchText = $event"
/>
來分析組件props 需要有一個'modelValue' ,組件的事件 $emit 需要有一個'update:modelValue'組件寫法
app.component('custom-input', {
props: ['modelValue'],
emits: ['update:modelValue'],
template: `
<input
:value="modelValue"
@input="$emit('update:modelValue', $event.target.value)"
>
`
})
2.v-model 可以定義多個寫法'<ChildComponent v-model:title="pageTitle" v-model:content="pageContent" />'
是一下的簡寫
<ChildComponent
:title="pageTitle"
@update:title="pageTitle = $event"
:content="pageContent"
@update:content="pageContent = $event"
/>
就是說明你的 props 定義['title','content'],對應的$emits定義['update:title'.'update:content'],事件綁定觸發$emits
就看項目需要可以是'input、change、input'
~~~
>[danger] ##### 使用
~~~
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!--組件疑問 全局注冊和局部注冊-->
<div id="app">
{{searchText}}
<custom-input v-model="searchText"></custom-input>
</div>
<script src="https://unpkg.com/vue@next"></script>
<script>
const {createApp} = Vue
const app = createApp({
data(){
return {
searchText:'11'
}
},
methods:{
fontSize(){
console.log("1")
}
},
})
app.component('custom-input', {
props: ['modelValue'],
emits: ['update:modelValue'],
template: `
<input
:value="modelValue"
@input="$emit('update:modelValue', $event.target.value)"
>
`
})
app.mount('#app')
</script>
</body>
</html>
~~~
>[danger] ##### 上面的案例官方給的另一種思路
~~~
1.使用計算屬性利用get 方法應返回 modelValue property,或用于綁定的任何 property,
set 方法應為該 property 觸發相應的 $emit。
~~~
~~~
app.component('custom-input', {
props: ['modelValue'],
emits: ['update:modelValue'],
template: `
<input v-model="value">
`,
computed: {
value: {
get() {
return this.modelValue
},
set(value) {
this.$emit('update:modelValue', value)
}
}
}
})
~~~
>[info] ## 官方給的遷移策略
我們推薦:
* 檢查你的代碼庫中所有使用`.sync`的部分并將其替換為`v-model`:
~~~
<ChildComponent :title.sync="pageTitle" />
<!-- 替換為 -->
<ChildComponent v-model:title="pageTitle" />
~~~
* 對于所有不帶參數的`v-model`,請確保分別將 prop 和 event 命名更改為`modelValue`和`update:modelValue`
~~~
<ChildComponent v-model="pageTitle" />
~~~
~~~
// ChildComponent.vue
export default {
props: {
modelValue: String // 以前是`value:String`
},
emits: ['update:modelValue'],
methods: {
changePageTitle(title) {
this.$emit('update:modelValue', title) // 以前是 `this.$emit('input', title)`
}
}
}
~~~
- 官網給的工具
- 聲明vue2 和 vue3
- 指令速覽
- Mustache -- 語法
- v-once -- 只渲染一次
- v-text -- 插入文本
- v-html -- 渲染html
- v-pre -- 顯示原始的Mustache標簽
- v-cloak -- 遮蓋
- v-memo(新)-- 緩存指定值
- v-if/v-show -- 條件渲染
- v-for -- 循環
- v-bind -- 知識
- v-bind -- 修飾符
- v-on -- 點擊事件
- v-model -- 雙向綁定
- 其他基礎知識速覽
- 快速使用
- 常識知識點
- key -- 作用 (后續要更新)
- computed -- 計算屬性
- watch -- 偵聽
- 防抖和節流
- vue3 -- 生命周期
- vue-cli 和 vite 項目搭建方法
- vite -- 導入動態圖片
- 組件
- 單文件組件 -- SFC
- 組件通信 -- porp
- 組件通信 -- $emit
- 組件通信 -- Provide / Inject
- 組件通信 -- 全局事件總線mitt庫
- 插槽 -- slot
- 整體使用案例
- 動態組件 -- is
- keep-alive
- 分包 -- 異步組價
- mixin -- 混入
- v-model-- 組件
- 使用計算屬性
- v-model -- 自定義修飾符
- Suspense -- 實驗屬性
- Teleport -- 指定掛載
- 組件實例 -- $ 屬性
- Option API VS Composition API
- Setup -- 組合API 入口
- api -- reactive
- api -- ref
- 使用ref 和 reactive 場景
- api -- toRefs 和 toRef
- api -- readonly
- 判斷性 -- API
- 功能性 -- API
- api -- computed
- api -- $ref 使用
- api -- 生命周期
- Provide 和 Inject
- watch
- watchEffect
- watch vs. watchEffect
- 簡單使用composition Api
- 響應性語法糖
- css -- 功能
- 修改css -- :deep() 和 var
- Vue3.2 -- 語法
- ts -- vscode 配置
- attrs/emit/props/expose/slots -- 使用
- props -- defineProps
- props -- defineProps Ts
- emit -- defineEmits
- emit -- defineEmits Ts
- $ref -- defineExpose
- slots/attrs -- useSlots() 和 useAttrs()
- 自定義指令
- Vue -- 插件
- Vue2.x 和 Vue3.x 不同點
- $children -- 移除
- v-for 和 ref
- attribute 強制行為
- 按鍵修飾符
- v-if 和 v-for 優先級
- 組件使用 v-model -- 非兼容
- 組件
- h -- 函數
- jsx -- 編寫
- Vue -- Router
- 了解路由和vue搭配
- vueRouter -- 簡單實現
- 安裝即使用
- 路由懶加載
- router-view
- router-link
- 路由匹配規則
- 404 頁面配置
- 路由嵌套
- 路由組件傳參
- 路由重定向和別名
- 路由跳轉方法
- 命名路由
- 命名視圖
- Composition API
- 路由守衛
- 路由元信息
- 路由其他方法 -- 添加/刪除/獲取
- 服務器配置映射
- 其他
- Vuex -- 狀態管理
- Option Api -- VUEX
- composition API -- VUEX
- module -- VUEX
- 刷新后vuex 數據同步
- 小技巧
- Pinia -- 狀態管理
- 開始使用
- pinia -- state
- pinia -- getter
- pinia -- action
- pinia -- 插件 ??
- Vue 源碼解讀
- 開發感悟
- 練手項目