Vm 實(shí)例屬性/方法

實(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()
      })
    }
  }
})
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 今天天氣好晴朗,氣溫也漸漸地變暖了。我的心情沒(méi)那么好,感冒又加重了,今晨周會(huì)時(shí),領(lǐng)導(dǎo)傳達(dá)會(huì)議精神,從現(xiàn)在開(kāi)...
    劉學(xué)穎媽媽閱讀 215評(píng)論 0 0
  • 去年的時(shí)候到處都風(fēng)靡著王者農(nóng)藥,終于在室友的影響下,也順利入坑了,還記得那天是2017年1月15號(hào)很順利的經(jīng)過(guò)了新...
    白風(fēng)閱讀 238評(píng)論 0 0
  • 一個(gè)pscc的插件 可以識(shí)別圖層的樣式并返回css3的格式css3Ps下載后解壓,文件放到pscc安裝目錄的下Re...
    727上上上閱讀 212評(píng)論 0 0
  • 一年余下的,12月的小尾巴, 這一天,圣誕簇?fù)碇螅?顯得有點(diǎn)稀松平常了,, 大人們奔赴忙工作, 小朋友們讀書(shū)上課...
    月半初上閱讀 483評(píng)論 0 0

友情鏈接更多精彩內(nèi)容