實(shí)例屬性#
$data
Vue 實(shí)例觀察的數(shù)據(jù)對(duì)象。Vue 實(shí)例代理了對(duì)其 data 對(duì)象屬性的訪問(wèn)。
var data = { a: 1 }
// 直接創(chuàng)建一個(gè)實(shí)例
var vm = new Vue({
data: data
})
vm.a // => 1
vm.$data === data // => true
// Vue.extend() 中 data 必須是函數(shù)
var Component = Vue.extend({
data: function () {
return { a: 1 }
}
})
$props
一個(gè)對(duì)象,代表當(dāng)前組件收到的 props。Vue 實(shí)例代理訪問(wèn)到這個(gè) props 對(duì)象的屬性們。
$el
Vue 實(shí)例使用的根 DOM 元素。
$options
用于當(dāng)前 Vue 實(shí)例的初始化選項(xiàng)。需要在選項(xiàng)中包含自定義屬性時(shí)會(huì)有用
new Vue({
customOption: 'foo',
created: function () {
console.log(this.$options.customOption) // => 'foo'
}
})
$parent
父實(shí)例,如果當(dāng)前實(shí)例有的話。
$root
當(dāng)前組件樹(shù)的根 Vue 實(shí)例。如果當(dāng)前實(shí)例沒(méi)有父實(shí)例,此實(shí)例將會(huì)是其自已。
$children
當(dāng)前實(shí)例的直接子組件。需要注意 $children 并不保證順序,也不是響應(yīng)式的。如果你發(fā)現(xiàn)自己正在嘗試使用
$children 來(lái)進(jìn)行數(shù)據(jù)綁定,考慮使用一個(gè)數(shù)組配合 v-for 來(lái)生成子組件,并且使用 Array 作為真正的來(lái)源。
$slots
用于以編程式來(lái)訪問(wèn) [由 slots 分發(fā)](https://vue.docschina.org/v2/guide/components.html#使用-slots-進(jìn)行內(nèi)
容分發(fā)) 的內(nèi)容。在 vm.$slots 上,有著與每個(gè) [具名 slot]
相應(yīng)的屬性(例如:在 `vm.$slots.foo` 上可
以找到 `slot="foo"` 中的內(nèi)容)。`default` 屬性是由所有匿名 slot 的 VNode 節(jié)點(diǎn)構(gòu)成的數(shù)組。
<blog-post>
<h1 slot="header">
About Me
</h1>
<p>Here's some page content, which will be included in vm.$slots.default, because it's not inside a named slot.</p>
<p slot="footer">
Copyright 2016 Evan You
</p>
<p>If I have some content down here, it will also be included in vm.$slots.default.</p>.
</blog-post>
Vue.component('blog-post', {
render: function (createElement) {
var header = this.$slots.header
var body = this.$slots.default
var footer = this.$slots.footer
return createElement('div', [
createElement('header', header),
createElement('main', body),
createElement('footer', footer)
])
}
})
$scopedSlots
用來(lái)訪問(wèn)。對(duì)于包括 `默認(rèn) slot` 在內(nèi)的每一個(gè) slot, 該對(duì)象都包含一個(gè)返回相應(yīng) VNode 的函數(shù)。
$refs
一個(gè)對(duì)象,包含 DOM 元素和組件實(shí)例,通過(guò)ref注冊(cè)。
$isServer
當(dāng)前 Vue 實(shí)例是否運(yùn)行于服務(wù)器。
$attrs
含了父作用域中不被認(rèn)為 (且不預(yù)期為) props 的特性綁定 (class 和 style 除外)。當(dāng)一個(gè)組件沒(méi)有聲明任何
props 時(shí),這里會(huì)包含所有父作用域的綁定 (class 和 style 除外),并且可以通過(guò) v-bind="$attrs" 傳入內(nèi)部組
件——在創(chuàng)建高階組件(higher-order component)時(shí)非常有用。
$listenners
包括父作用域中的 v-on 事件監(jiān)聽(tīng)器(但不包括添加了 .native 修飾器的那些事件監(jiān)聽(tīng)器)??梢酝ㄟ^(guò) v-
on="$listeners",將這些事件監(jiān)聽(tīng)器向下傳入到組件內(nèi)部,在創(chuàng)建透明容器組件(transparent wrapper component)時(shí)非常有用。
實(shí)例方法#
$watch
觀察 Vue 實(shí)例變化的一個(gè)表達(dá)式或計(jì)算屬性函數(shù)?;卣{(diào)函數(shù)得到的參數(shù)為新值和舊值。表達(dá)式只接受監(jiān)督的鍵路徑。對(duì)于更復(fù)雜的表達(dá)式,用一個(gè)函數(shù)取代。
// 鍵路徑
vm.$watch('a.b.c', function (newVal, oldVal) {
// 做點(diǎn)什么
})
// 函數(shù)
vm.$watch(
function () {
return this.a + this.b
},
function (newVal, oldVal) {
// 做點(diǎn)什么
}
)
$set
vm.$set( target, key, value )
this.$set(this.someObject,'b',2)
$delete
vm.$delete( target, key )
$emit
Vue.component('welcome-button', {
template: `
<button v-on:click="$emit('welcome')">
Click me to be welcomed
</button>
`
})
<div id="emit-example-simple">
<welcome-button v-on:welcome="sayHi"></welcome-button>
</div>
$nextTick
將回調(diào)延遲到下次 DOM 更新循環(huán)之后執(zhí)行。在修改數(shù)據(jù)之后立即使用它,然后等待 DOM 更新。它跟全局方
法 Vue.nextTick 一樣,不同的是回調(diào)的 this 自動(dòng)綁定到調(diào)用它的實(shí)例上。
new Vue({
// ...
methods: {
// ...
example: function () {
// 修改數(shù)據(jù)
this.message = 'changed'
// DOM 還沒(méi)有更新
this.$nextTick(function () {
// DOM 現(xiàn)在更新了
// `this` 綁定到當(dāng)前實(shí)例
this.doSomethingElse()
})
}
}
})