Vue核心知識 Vue的元件之自定義雙向繫結

2021-08-27 11:52:04 字數 1030 閱讀 7020

實現資料雙向繫結

在元件內,新增 props;元件修改資料時 emit 事件,並把改的新值傳出去;從而實現資料的雙向繫結。

import vue from 'vue'

const component =

}}new vue(,

el: '#root',

data ()

},template: `

:value="value"

@input="value = arguments[0]"

>

comp-one>

div>

`})

與上述例子,效果相同。

v-model 相當於給元件增加了 乙個 props 屬性以及乙個事件監聽,實現了雙向繫結的邏輯。本質上與上個例子一樣。

template: `

v-model="value">

comp-one>

div>

`

當元件使用 value 實現了乙個功能,但又需要實現雙向繫結,這時就會有衝突。

通過 定義元件時的model 選項,從而實現了自定義元件 v-model 功能。

類似編輯器的功能,可能用 v-model 的方式會更好,可以通過這種方式使用。

import vue from 'vue'

const component = ,

props: ['value1'],

template: `

type="text" @input="handleinput"

:value="value1"/>

div>

`,methods:

}}new vue(,

el: '#root',

data ()

},template: `

v-model="value">

comp-one>

div>

`})

Vue核心知識 Vue的元件之render函式

元件中的 template 會被編譯成 render function。下例中,直接用 render function 代替 template,結果相同。import vue from vue const component this.slots.default data value compone...

Vue核心知識 Vue的資料繫結

中可以進行簡單的一元 二元運算,但不能寫語句 import vue from vue new vue data 呼叫全域性變數 template vue 中,在模板裡可以訪問的,乙個是繫結到 this 上的所有值,是可以訪問到的 乙個是 vue 自建的白名單,預設的 js 的全域性物件,也是可以訪問...

掌握vue核心概念之元件

推薦駝峰命名 或者 使用 還是看個人看法習慣 const globalcomponent template 這是乙個全域性元件 const localcomponent template 這是乙個區域性元件 vue.component globalcomponent globalcomponent ...