首页 > 技术文章 > vue对特殊特性的研究

zhouyideboke 2018-08-20 19:00 原文

key

  • 预期:number | string

    key 的特殊属性主要用在 Vue 的虚拟 DOM 算法,在新旧 nodes 对比时辨识 VNodes。如果不使用 key,Vue 会使用一种最大限度减少动态元素并且尽可能的尝试修复/再利用相同类型元素的算法。使用 key,它会基于 key 的变化重新排列元素顺序,并且会移除 key 不存在的元素。

    有相同父元素的子元素必须有独特的 key。重复的 key 会造成渲染错误。

    最常见的用例是结合 v-for

    <ul>
    <li v-for="item in items" :key="item.id">...</li>
    </ul>

    它也可以用于强制替换元素/组件而不是重复使用它。当你遇到如下场景时它可能会很有用:

    • 完整地触发组件的生命周期钩子
    • 触发过渡

    例如:

    <transition>
    <span :key="text">{{ text }}</span>
    </transition>

    当 text 发生改变时,<span> 会随时被更新,因此会触发过渡。

ref

  • 预期:string

    ref 被用来给元素或子组件注册引用信息。引用信息将会注册在父组件的 $refs 对象上。如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;如果用在子组件上,引用就指向组件实例:

    <!-- `vm.$refs.p` will be the DOM node -->
    <p ref="p">hello</p>

    <!-- `vm.$refs.child` will be the child component instance -->
    <child-component ref="child"></child-component>

    当 v-for 用于元素或组件的时候,引用信息将是包含 DOM 节点或组件实例的数组。

    关于 ref 注册时间的重要说明:因为 ref 本身是作为渲染结果被创建的,在初始渲染的时候你不能访问它们 - 它们还不存在!$refs 也不是响应式的,因此你不应该试图用它在模板中做数据绑定。

  • 参考:子组件 Refs

slot

  • 预期:string

    用于标记往哪个具名插槽中插入子组件内容。

    详细用法,请参考下面指南部分的链接。

  • 参考:具名插槽

slot-scope

2.5.0 新增

  • 预期:function argument expression

  • 用法:

    用于将元素或组件表示为作用域插槽。特性的值应该是可以出现在函数签名的参数位置的合法的 JavaScript 表达式。这意味着在支持的环境中,你还可以在表达式中使用 ES2015 解构。它在 2.5.0+ 中替代了 scope

    此属性不支持动态绑定。

  • 参考:Scoped Slots

scope replaced

用于表示一个作为带作用域的插槽的 <template> 元素,它在 2.5.0+ 中被 slot-scope 替代。

  • 用法:

    除了 scope 只可以用于 <template> 元素,其它和 slot-scope 都相同。

is

  • 预期:string | Object (组件的选项对象)

    用于动态组件且基于 DOM 内模板的限制来工作。

    示例:

    <!-- 当 `currentView` 改变时,组件也跟着改变 -->
    <component v-bind:is="currentView"></component>

    <!-- 这样做是有必要的,因为 `<my-row>` 放在一个 -->
    <!-- `<table>` 内可能无效且被放置到外面 -->
    <table>
    <tr is="my-row"></tr>
    </table>

    更多的使用细节,请移步至下面的链接。

  • See also:

内置的组件

component

  • Props:

    • is - string | ComponentDefinition | ComponentConstructor
    • inline-template - boolean
  • 用法:

    渲染一个“元组件”为动态组件。依 is 的值,来决定哪个组件被渲染。

    <!-- 动态组件由 vm 实例的属性值 `componentId` 控制 -->
    <component :is="componentId"></component>

    <!-- 也能够渲染注册过的组件或 prop 传入的组件 -->
    <component :is="$options.components.child"></component>
  • 参考:动态组件

 

transition

  • Props:

    • name - string,用于自动生成 CSS 过渡类名。例如:name: 'fade' 将自动拓展为.fade-enter.fade-enter-active等。默认类名为 "v"
    • appear - boolean,是否在初始渲染时使用过渡。默认为 false
    • css - boolean,是否使用 CSS 过渡类。默认为 true。如果设置为 false,将只通过组件事件触发注册的 JavaScript 钩子。
    • type - string,指定过渡事件类型,侦听过渡何时结束。有效值为 "transition" 和 "animation"。默认 Vue.js 将自动检测出持续时间长的为过渡事件类型。
    • mode - string,控制离开/进入的过渡时间序列。有效的模式有 "out-in" 和 "in-out";默认同时生效。
    • enter-class - string
    • leave-class - string
    • appear-class - string
    • enter-to-class - string
    • leave-to-class - string
    • appear-to-class - string
    • enter-active-class - string
    • leave-active-class - string
    • appear-active-class - string
  • 事件:

    • before-enter
    • before-leave
    • before-appear
    • enter
    • leave
    • appear
    • after-enter
    • after-leave
    • after-appear
    • enter-cancelled
    • leave-cancelled (v-show only)
    • appear-cancelled
  • 用法:

    <transition> 元素作为单个元素/组件的过渡效果。<transition> 只会把过渡效果应用到其包裹的内容上,而不会额外渲染 DOM 元素,也不会出现在检测过的组件层级中。

    <!-- 简单元素 -->
    <transition>
    <div v-if="ok">toggled content</div>
    </transition>

    <!-- 动态组件 -->
    <transition name="fade" mode="out-in" appear>
    <component :is="view"></component>
    </transition>

    <!-- 事件钩子 -->
    <div id="transition-demo">
    <transition @after-enter="transitionComplete">
    <div v-show="ok">toggled content</div>
    </transition>
    </div>

 

 

  • new Vue({
    ...
    methods: {
    transitionComplete: function (el) {
    // 传入 'el' 这个 DOM 元素作为参数。
    }
    }
    ...
    }).$mount('#transition-demo')
  • 参考:过渡:进入,离开和列表

 

推荐阅读