Vue 实例 property 属性

发布于 2024-11-25 02:19:16 字数 4030 浏览 16 评论 0

vm.$data

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

vm.$props

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

vm.$el

Vue 实例使用的 根 DOM 元素

vm.$options

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

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

vm.$parent

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

vm.$root

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

vm.$children

类型Array<Vue instance>

当前实例的直接子组件。 需要注意 $children 并不保证顺序,也不是响应式的。

如果你正在尝试使用 $children 来进行数据绑定,考虑使用一个数组配合 v-for 来生成子组件,并且使用 Array 作为真正的来源。

vm.$slots

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

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

  • 请注意插槽 不是 响应性的。
  • 在使用 渲染函数 书写一个组件时,访问 vm.$slots 最有帮助。
    <blog-post>
      <template v-slot:header>
        <h1>About Me</h1>
      </template>
    
      <p>Here's some page content, which will be included in vm.$slots.default, because it's not inside a named slot.</p>
    
      <template v-slot:footer>
        <p>Copyright 2016 Evan You</p>
      </template>
    
      <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)
        ])
      }
    })
    

vm.$scopedSlots

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

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

  • vm.$scopedSlots 在使用 渲染函数 开发一个组件时特别有用。
  • 作用域插槽函数现在保证返回一个 VNode 数组,除非在返回值无效的情况下返回 undefined
  • 所有的 $slots 现在都会作为函数暴露在 $scopedSlots 中。如果你在使用渲染函数,不论当前插槽是否带有作用域,我们都推荐始终通过 $scopedSlots 访问它们。这不仅仅使得在未来添加作用域变得简单,也可以让你最终轻松迁移到所有插槽都是函数的 Vue 3。

vm.$refs

一个对象,持有注册过 ref attribute 的所有 DOM 元素和组件实例。

vm.$isServer

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

vm.$attrs

类型{ [key: string]: string }

包含了父作用域中不作为 prop 被识别 (且获取) 的 attribute 绑定 ( classstyle 除外)。

当一个组件没有声明任何 prop 时,这里会包含所有父作用域的绑定 ( classstyle 除外),并且可以通过 v-bind="$attrs" 传入内部组件——在创建高级别的组件时非常有用。

vm.$listeners

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

包含了父作用域中的 (不含 .native 修饰器的) v-on 事件监听器。

它可以通过 v-on="$listeners" 传入内部组件——在创建更高层次的组件时非常有用。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

上一篇:

下一篇:

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

墨小墨

暂无简介

文章
评论
25 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文