插槽(Slot)
定义一个名 child 子组件,为该子组件添加内容应该在子组件的 template 中定义,直接在父组件的<child>
标签中定义的内容不会被渲染。
在子组件中通过加入<slot>
元素占位,便能够渲染父组件中子组件标签中的内容了。
插槽内容
插槽可以有默认内容,当在父组件中没有提供内容的时候,来进行显示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <button type="submit"> <slot>Submit</slot> </button>
1. <submit-button></submit-button> ⬇️ <button type="submit"> Submit </button>
2. <submit-button> Save </submit-button> ⬇️ <button type="submit"> Save </button>
|
作用域
父级模板里的所有内容都是在父级作用域中编译的;子模板里的所有内容都是在子作用域中编译的。
具名插槽
试想,我们有一个带有如下模版的<base-layout>
组件
1 2 3 4 5 6 7 8 9 10 11
| <div class="container"> <header> </header> <main> </main> <footer> </footer> </div>
|
可以看到,在组件中显示的内容是划分不同的部位的,这个时候就需要使用到<slot>
元素的一个特有的属性:name
来实现了。这个特性可以用来定义额外的插槽。
1 2 3 4 5 6 7 8 9 10 11
| <div class="container"> <header> <slot name="header"></slot> </header> <main> <slot></slot> </main> <footer> <slot name="footer"></slot> </footer> </div>
|
一个不带 name 的 <slot>
出口会带有隐含的名字“default”。
在向具名插槽提供内容的时候,我们可以在一个 <template>
元素上使用 v-slot
指令,并以 v-slot
的参数的形式提供其名称:
1 2 3 4 5 6 7 8 9 10 11 12
| <base-layout> <template v-slot:header> <h1>Here might be a page title</h1> </template>
<p>A paragraph for the main content.</p> <p>And another one.</p>
<template v-slot:footer> <p>Here's some contact info</p> </template> </base-layout>
|
现在 <template>
元素中的所有内容都将会被传入相应的插槽。任何没有被包裹在带有 v-slot
的 <template>
中的内容都会被视为默认插槽的内容。
当然,也可以将默认插槽的内容通过v-slot:default
包裹起来。
v-slot
只能添加在一个 <template>
上
作用域插槽
当我们希望能够让插槽内容能够访问子组件中才有的数据时,我们可以将数据作为一个<slot>
元素的特性绑定上去
1 2 3 4 5
| <span> <slot v-bind:user="user"> {{ user.name }} </slot> </span>
|
绑定在<slot>
元素上的特性被称为插槽 prop。此时我们在父组件中通过给v-slot
带一个值来定义我们提供的插槽 prop 的名字。
1 2 3 4 5
| <current-user> <template v-slot:default="slotProps"> {{ slotProps.user.age }} </template> </current-user>
|
独占默认插槽的缩写语法
当被提供的内容只有默认插槽时,上面的写法可以被简化来写
1 2 3 4 5 6 7 8 9
| <current-user v-slot:default="slotProps"> {{ slotProps.user.firstName }} </current-user>
<current-user v-slot="slotProps"> {{ slotProps.user.firstName }} </current-user>
|
需要注意两点:
- 简化写法不能和具名插槽混用,作用域不明确
- 出现多个插槽时,所有插槽都使用完整的基于
<template>
语法
解构插槽 Prop
作用域插槽的内部工作原理是将你的插槽内容包括在一个传入单个参数的函数里:
1 2 3
| function (slotProps) { }
|
这意味着 v-slot
的值实际上可以是任何能够作为函数定义中的参数的 JavaScript
表达式。所以在支持的环境下 (单文件组件或现代浏览器),你也可以使用 ES2015 解构来传入具体的插槽 prop
,如下:
1 2 3 4 5 6 7
| <current-user v-slot:default="slotProps"> {{ slotProps.user.firstName }} </current-user> ⬇️ <current-user v-slot="{ user }"> {{ user.firstName }} </current-user>
|
使用场景举例
插槽 prop
允许我们将插槽转换为可复用的模板,这些模板可以基于输入的 prop
渲染出不同的内容。
这在设计封装数据逻辑同时允许父级组件自定义部分布局的可复用组件时是最有用的。
动态插槽名
动态指令参数也可以用在 v-slot
上,来定义动态的插槽名
1 2 3 4 5
| <base-layout> <template v-slot:[dynamicSlotName]> ... </template> </base-layout>
|
具名插槽缩写
v-slot
可以缩写为#
。
缩写方式只有在有参数的时候才可以使用
1 2 3 4 5 6 7 8 9
| <current-user #="{ user }"> {{ user.firstName }} </current-user>
<current-user #default="{ user }"> {{ user.firstName }} </current-user>
|
动态组件&keep-alive
当在这些组件之间切换的时候,你有时会想保持这些组件的状态,以避免反复重渲染导致的性能问题。为了解决这个问题,我们可以用一个<keep-alive>
元素将动态组件包裹起来
1 2 3 4
| <keep-alive> <component v-bind:is="currentTabComponent"></component> </keep-alive>
|
注意这个 <keep-alive>
要求被切换到的组件都有自己的名字,不论是通过组件的 name 选项还是局部/全局注册。
更加详细的说明在我们之后的实战过程中遇到的话,再进行专门解说。
异步组件
在实际的项目过程中,我们往往会将一系列的功能分割成一个个小的代码块,希望只有在需要的时候才去加载。为了达成这个目的,Vue 允许我们以一个工厂函数的方式定义我们的组件,这个工厂函数会异步解析组件的定义。
Vue 只有在这个组件需要渲染的时候才会触发这个工厂函数,而且会把结果缓存起来供之后使用。
1 2 3 4 5 6 7 8
| Vue.component('async-example', function (resolve, reject) { setTimeout(function () { resolve({ template: '<div>I am async!</div>' }) }, 1000) })
|
其实,这个过程有些类似于我们设计一个异步函数,这个工厂函数会收到一个 resolve 回调,这个回调在我们从服务器获取到组件定义的时候被调用,当加载失败的时候我们也可以调用 reject(reason)。
一个推荐的做法是异步组件和 webpack 的 code-splitting 功能结合使用
1 2 3 4 5 6
| Vue.component('async-webpack-example', function (resolve) { // 这个特殊的 `require` 语法将会告诉 webpack // 自动将你的构建代码切割成多个包,这些包 // 会通过 Ajax 请求加载 require(['./my-async-component'], resolve) })
|
同样,也可以在工厂函数中返回一个Promise
1 2 3 4 5
| Vue.component( 'async-webpack-example', // 这个 `import` 函数会返回一个 `Promise` 对象。 () => import('./my-async-component') )
|
处理加载状态
上面的工厂函数可以返回一个下面格式的对象
1 2 3 4 5 6 7 8 9 10 11 12 13
| const AsyncComponent = () => ({ // 需要加载的组件 (应该是一个 `Promise` 对象) component: import('./MyComponent.vue'), // 异步组件加载时使用的组件 loading: LoadingComponent, // 加载失败时使用的组件 error: ErrorComponent, // 展示加载时组件的延时时间。默认值是 200 (毫秒) delay: 200, // 如果提供了超时时间且组件加载也超时了, // 则使用加载失败时使用的组件。默认值是:`Infinity` timeout: 3000 })
|
小结
本篇我们主要围绕着 Vue 组件中的插槽和相关动态组件、异步组件的内容进行了梳理。通过这两篇的整理,我们对于 Vue 组件有了一个比较整体的了解。后续我们会在实战过程中针对具体的点再进行详细的说明。