>[success] # v-model 組件
1. 在`input`中可以使用`v-model`來完成**雙向綁定**,組件也可以使用`v-model`形式進行雙向綁定和` vue2.x` 時候只能定義一個,因此往往使用`.sync` 語法糖進行多個綁定,在`vue3.x` 開始,`v-model` 用法也升級了可以進行多個綁定,因此`.sync`語法糖也在3移除
2. 在組件使用`v-model`他的語法糖 需要定義的`props `屬性名 `modelValue` 觸發拋出的事件名`update:modelValue`
~~~ html
<input v-model="searchText" />
<!--等價于:-->
<input :value="searchText" @input="searchText = $event.target.value" />
~~~
~~~html
<custom-input v-model="searchText" />
<!--等價于:-->
<custom-input
:model-value="searchText"
@update:model-value="searchText = $event"
></custom-input>
~~~
* CustomInput 組件 定義
~~~html
<!-- CustomInput.vue -->
<script>
export default {
props: ['modelValue'],
emits: ['update:modelValue']
}
</script>
<template>
<input
:value="modelValue"
@input="$emit('update:modelValue', $event.target.value)"
/>
</template>
~~~
**通過案例對比看也就`value` => `model-value`,`input` => ` @update:model-value`**
>[danger] ##### 在對比來看不用v-model 語法糖來寫
~~~
1. 案例就是常見的,父組件傳值給子組件,子組件觸發事件改變父組件值,從而達到改變子組件值
~~~
* 父
~~~
<template>
<children-todo :model-value="name" @update:model-value="name = $event">
</children-todo>
</template>
<script>
import childrenTodo from './components/children-todo.vue'
export default {
name: 'App',
components: {
childrenTodo,
},
data() {
return {
name: '父組件',
}
},
}
</script>
<style></style>
~~~
* 子
~~~
<template>
<div class="content">
{{ modelValue }}
<button @click="change">改變</button>
</div>
</template>
<script>
export default {
props: ['modelValue'],
emits: ['update:modelValue'],
methods: {
change() {
this.$emit('update:modelValue', '1', '2')
},
},
}
</script>
~~~
>[danger] ##### 綁定多個v-model
1. 原來`2.x`由于 `v-model` 在一個組件只能出現一次,因此出現了使用`.sync`語法糖來彌補這個問題,現在`3.x`的`v-model `支持綁定多個因此`.sync` 也不在支持
2. 用法只要聲明`v-model` 所屬即可,`v-model:名字` 例如 組件默認的`v-model` 其實是`v-model:modelValue` 的縮寫因此在子組件接受時候`props` 為`props: ['modelValue']` , 事件為`emits: ['update:modelValue']`,那如果綁定值定義如下`v-model:title` 則`props: ['title']` ,`emits: ['update:title']`
* 父
~~~html
<template>
<children-todo v-model="name" v-model:title="title"> </children-todo>
</template>
<script>
import childrenTodo from './components/children-todo.vue'
export default {
name: 'App',
components: {
childrenTodo,
},
data() {
return {
title: '父組件標題',
name: '父組件',
}
},
}
</script>
<style></style>
~~~
* 子
~~~html
<template>
<div class="content">
{{ modelValue }}
{{ title }}
<button @click="change">改變</button>
</div>
</template>
<script>
export default {
props: ['modelValue', 'title'],
emits: ['update:modelValue', 'update:title'],
methods: {
change() {
this.$emit('update:modelValue', '1')
this.$emit('update:title', 'title')
},
},
}
</script>
<style scoped>
.content {
display: flex;
}
.left,
.right {
width: 80px;
}
.left,
.right,
.center {
height: 44px;
}
.left,
.right {
width: 80px;
background-color: red;
}
.center {
flex: 1;
background-color: blue;
}
</style>
~~~
>[info] ## 官方
[配合 v-model 使用](https://cn.vuejs.org/guide/components/events.html#usage-with-v-model)
- 官網給的工具
- 聲明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 源碼解讀
- 開發感悟
- 練手項目