Skip to content

实例属性

vm.$data

类型:Object

详细:

Vue 实例观察的数据对象。Vue 实例代理了对其 data 对象属性的访问。

vm.$props

类型:Object

详细:

当前组件接收到的 props 对象。Vue 实例代理了对其 props 对象属性的访问。

vm.$el

类型:HTMLElement

只读

详细:

Vue 实例使用的根 DOM 元素。

vm.$options

类型:Object

只读

详细:

用于当前 Vue 实例的初始化选项。需要在选项中包含自定义属性时会有用处:

js
new Vue({
  customOption: 'foo',
  created() {
    console.log(this.$options.customOption) // 'foo'
  }
})

vm.$parent

类型:Vue instance

只读

详细:

父实例,如果当前实例有的话。

vm.$root

类型:Vue instance

只读

详细:

当前组件树的根 Vue 实例。如果当前实例没有父实例,此实例将会是其自己。

vm.$children

类型:Array<Vue instance>

只读

详细:

当前实例的直接子组件。需要注意 $children 并不保证顺序,也不是响应式的。如果你发现自己正在尝试使用 $children 来进行数据绑定,考虑使用一个数组配合 v-for 来生成子组件,并且使用 Array 作为真正的来源。

vm.$slots

类型:{ [name: string]: ?Array<VNode> }

只读

详细:

用来访问被插槽分发的内容。每个具名插槽 有其相应的属性 (例如:slot="foo" 中的内容将会在 vm.$slots.foo 中被找到)。default 属性包括了所有没有被包含在具名插槽中的节点。

在使用渲染函数书写一个组件时,访问 vm.$slots 最有帮助。

html
&lt;blog-post>
  &lt;h1 slot="header">About Me&lt;/h1>
  &lt;p>Here's some page content, which will be included in vm.$slots.default, because it's not inside a named slot.&lt;/p>
  &lt;p slot="footer">
    Copyright 2016 Evan You
  &lt;/p>
  &lt;p>If I have some content down here, it will also be included in vm.$slots.default.&lt;/p>.
&lt;/blog-post>
&lt;script>
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)
    ])
  }
})
&lt;/script>

vm.$scopedSlots

类型:{ [name: string]: props => VNode | Array<VNode> }

只读

详细:

用来访问作用域插槽。对于包括 默认 slot 在内的每一个插槽,该对象都包含一个返回相应 VNode 的函数。

vm.$scopedSlots 在使用渲染函数开发一个组件时特别有用。

vm.$refs

类型:Object

只读

详细:

一个对象,持有已注册过 ref 的所有子组件。

参考:

子组件引用

特殊特性 - ref

vm.$isServer

类型:boolean

只读

详细:

当前 Vue 实例是否运行于服务器。

参考:服务端渲染

vm.$attrs

类型:

只读

详细:

包含了父作用域中不被认为 (且不预期为) props 的特性绑定 (class 和 style 除外)。当一个组件没有声明任何 props 时,这里会包含所有父作用域的绑定 (class 和 style 除外),并且可以通过 v-bind="$attrs" 传入内部组件——在创建更高层次的组件时非常有用。

vm.$listeners

类型:{ [key: string]: Function | Array<Function> }

只读

详细:

包含了父作用域中的 (不含 .native 修饰器的) v-on 事件监听器。它可以通过 v-on="$listeners" 传入内部组件——在创建更高层次的组件时非常有用。

共 20 个模块,1301 篇 Markdown 文档。