一, 深度作用选择器(>>> )
严格来说, 这个应该是 vue-loader 的功能."vue-loader": "^12.2.0"
在项目开发中, 如果业务比较复杂, 特别像中台或 B 端功能页面都不可避免的会用到第三方组件库, 产品有时会想对这些组件进行一些 UI 方面的定制. 如果这些组件采用的是有作用域的 CSS, 父组件想要定制第三方组件的样式就比较麻烦了.
深度作用选择器 (>>> 操作符) 可以助你一臂之力.
- <template>
- <div>
- <h1 class="child-title">
如果你希望 scoped 样式中的一个选择器能够作用得 "更深", 例如影响子组件, 你可以使用>>> 操作
- </h1>
- </div>
- </template>
- <script>
- export default {
- name: 'child',
- data() {
- return {}
- }
- }
- </script>
- <!-- Add "scoped" attribute to limit CSS to this component only -->
- <style scoped>
- .child-title {
- font-size: 12px;
- }
- </style >
上面的 child 组件中 .child-title 的作用域 CSS 设定字体大小为 12px, 现在想在父组件中定制为大小 20px, 颜色为红色.
- <template>
- <div>
- <child class="parent-custom"></child>
- </div>
- </template>
- <script>
- import Child from './child'
- export default {
- name: 'parent',
- components: {
- Child
- },
- data() {
- return {}
- }
- }
- </script>
- <style>
- .parent-custom>>> .child-title {
- font-size:20px;
- color: red;
- }
- </style >
效果妥妥的. 但是别高兴太早, 注意到上面的 style 使用的是纯 CSS 语法, 如果采用 Less 语法, 你可能会收到一条 webpack 的报错信息.
- <style lang="less">
- .parent-custom {
- >>> .child-title {
- font-size:20px;
- color: red;
- }
- }
- </style>
- ERROR in ./~/CSS-loader!./~/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-960c5412","scoped":false,"hasInlineConfig":false}!./~/postcss-loader!./~/Less-loader!./~/vue-loader/lib/selector.JS?type=styles&index=0!./src/components/parent.vue
- Module build failed: Unrecognised input
- @ /src/components/parent.vue (line 22, column 6)
- near lines:
- .parent-custom {
- >>> .child-title {
font-size:20px;
上面的报错信息其实是 Less 语法不认识>>>.(Less 的 GitHub issue 上有人提议支持>>>操作符, 但本文使用的 v2.7.3 会有这个问题)
解决方案是采用的 Less 的转义 (scaping) 和变量插值(Variable Interpolation)
- <style lang="less">
- @deep: ~'>>>';
- .parent-custom {
- @{deep} .child-title {
- font-size:20px;
- color: red;
- }
- }
- </style >
对于其他的 CSS 预处理器, 因为没怎么用, 不妄加评论, 照搬一下文档的话.
有些像 Sass 之类的预处理器无法正确解析>>>. 这种情况下你可以使用 /deep/ 操作符取而代之 -- 这是一个>>> 的别名, 同样可以正常工作.
二, 组件配置项 inheritAttrs, 组件实例属性 $attrs 和 $listeners
2.4.0 新增
组件配置项 inheritAttrs
我们都知道假如使用子组件时传了 a,b,c 三个 prop, 而子组件的 props 选项只声明了 a 和 b, 那么渲染后 c 将作为 html 自定义属性显示在子组件的根元素上.
如果不希望这样, 可以设置子组件的配置项 inheritAttrs:false, 根元素就会干净多了.
- <script>
- export default {
- name: 'child',
- props: ['a','b'],
- inheritAttrs: false
- }
- </script >
组件实例属性 $attrs 和 $listeners
先看看 vm.$attrs 文档上是怎么说的
vm.$attrs
类型:{ [key: string]: string }
只读
包含了父作用域中不作为 prop 被识别 (且获取) 的特性绑定 (class 和 style 除外). 当一个组件没有声明任何 prop 时, 这里会包含所有父作用域的绑定 (class 和 style 除外), 并且可以通过 v-bind="$attrs" 传入内部组件 -- 在创建高级别的组件时非常有用.
归纳起来就是两点:
vm.$attrs 是组件的内置属性, 值是父组件传入的所有 prop 中未被组件声明的 prop(class 和 style 除外).
还是以前面的 child 组件举例
- // parent.vue
- <template>
- <div>
- <child
- class="parent-custom"
- a="a"
- b="b"
- c="c">
- </child>
- </div>
- </template>
- // child.vue
- <script>
- export default {
- name: 'child',
- props: ['a','b'],
- inheritAttrs: false,
- mounted() {
- // 控制台输出:
- // child:$attrs: {c: "c"}
- console.log('child:$attrs:', this.$attrs)
- }
- }
- </script >
组件可以通过在自己的子组件上使用 v-bind='attrs', 进一步把值传给自己的子组件. 也就是说子组件会把 attrs 的值当作传入的 prop 处理, 同时还要遵守第一点的规则.
- // parent.vue
- <template>
- <div>
- <child
- a="a"
- b="b"
- c="c">
- </child>
- </div>
- </template>
- // child.vue
- <template>
- <div>
- <grand-child
- v-bind="$attrs"
- d="d">
- </grand-child>
- </div>
- </template>
- <script>
- export default {
- name: 'child',
- props: ['a','b'],
- inheritAttrs: false
- }
- </script>
- // grandchild.vue
- <script>
- export default {
- name: 'grandchild',
- props: [],
- // props: ['c'],
- inheritAttrs: false,
- mounted() {
- // 控制台输出:
- // grandchild:$attrs: {d: "d", c: "c"}
- console.log('grandchild:$attrs:', this.$attrs)
- // 如果 props: ['c']
- // 控制台输出:
- // grandchild:$attrs: {d: "d"}
- }
- }
- </script >
- vm.$listeners
类型:{ [key: string]: Function | Array }
只读
包含了父作用域中的 (不含 .native 修饰器的) v-on 事件监听器. 它可以通过 v-on="$listeners" 传入内部组件 -- 在创建更高层次的组件时非常有用.
归纳起来也是两点:
vm.$listeners 是组件的内置属性, 它的值是父组件(不含 .native 修饰器的) v-on 事件监听器.
组件可以通过在自己的子组件上使用 v-on='$listeners', 进一步把值传给自己的子组件. 如果子组件已经绑定 listener 中同名的监听器, 则两个监听器函数会以冒泡的方式先后执行.
- // parent.vue
- <template>
- <div>
- <child @update="onParentUpdate"></child>
- </div>
- </template>
- <script>
- export default {
- name: 'parent',
- components: {
- Child
- },
- methods: {
- onParentUpdate() {
- console.log('parent.vue:onParentUpdate')
- }
- }
- }
- </script>
- // child.vue
- <template>
- <div>
- <grand-child
- @update="onChildUpdate"
- v-on="$listeners">
- </grand-child>
- </div>
- </template>
- <script>
- export default {
- name: 'child',
- components: {
- GrandChild
- },
- methods:{
- onChildUpdate() {
- console.log('child.vue:onChildUpdate')
- }
- }
- }
- </script>
- // grandchild.vue
- <script>
- export default {
- name: 'grandchild',
- mounted() {
- // 控制台输出:
- // grandchild:$listeners: {update: ƒ}
- console.log('grandchild:$listeners:',this.$listeners);
- // 控制台输出:
- // child.vue:onChildUpdate
- // parent.vue:onParentUpdate
- this.$listeners.update()
- }
- }
- </script >
三, 组件选项 provide/inject
2.2.0 新增
如果列举 Vue 组件之间的通信方法, 一般都会说通过 prop, 自定义事件, 事件总线, 还有 Vuex,provide/inject 提供了另一种方法.
这对选项需要一起使用, 以允许一个祖先组件向其所有子孙后代注入一个依赖, 不论组件层次有多深, 并在起上下游关系成立的时间里始终生效.
如果你熟悉 React, 这与 React 的上下文特性 (context) 很相似.
不过需要注意的是, 在文档中并不建议直接用于应用程序中.
provide 和 inject 主要为高阶插件 / 组件库提供用例. 并不推荐直接用于应用程序代码中.
- //parent.vue
- <template>
- <div>
- <child></child>
- </div>
- </template>
- <script>
- export default {
- name: 'parent',
- provide: {
- data: 'I am parent.vue'
- },
- components: {
- Child
- }
- }
- </script>
- // child.vue
- <template>
- <div>
- <grand-child></grand-child>
- </div>
- </template>
- <script>
- export default {
- name: 'child',
- components: {
- GrandChild
- }
- }
- </script>
- // grandchild.vue
- <script>
- export default {
- name: 'grandchild',
- inject: ['data'],
- mounted() {
- // 控制台输出:
- // grandchild:inject: I am parent.vue
- console.log('grandchild:inject:',this.data);
- }
- }
- </script >
provide 选项应该是一个对象或返回一个对象的函数. 该对象包含可注入其子孙的属性.
inject 选项应该是一个字符串数组或一个对象, 该对象的 key 代表了本地绑定的名称, value 就为 provide 中要取值的 key.
在 2.5.0 + 时对于 inject 选项为对象时, 还可以指定 from 来表示源属性, default 指定默认值(如果是非原始值要使用一个工厂方法).
- const Child = {
- inject: {
- foo: {
- from: 'bar',
- default: 'foo'
- //default: () => [1, 2, 3]
- }
- }
}
四, 作用域插槽 slot-scope
2.1.0 新增
在 2.5.0+,slot-scope 不再限制在 template 元素上使用, 而可以用在插槽内的任何元素或组件上.
作用域插槽的文档说明很详细. 下面举个例子来展示下应用场景.
列表页和编辑页对于数据的展示是一样的, 唯一的区别是在不同页面对于数据有不同的处理逻辑. 相同的数据展示这块就可抽取成一个组件, 不同的地方则可以借助作用域插槽实现.
- // data-show.vue
- <template>
- <div>
- <ul>
- <li v-for="item in list">
- <span>{{item.title}}</span>
- <slot v-bind:item="item">
- </slot>
- </li>
- </ul>
- </div>
- </template>
- // list.vue
- <template>
- <p > 列表页</p>
- <data-show :list="list">
- <template slot-scope="slotProps">
- <span v-if="slotProps.item.complete"></span>
- <span v-else>x</span>
- </template>
- </data-show>
- </template>
- // edit.vue
- <template>
- <p > 编辑页</p>
- <data-show :list="list">
- <template slot-scope="slotProps">
- <a v-if="slotProps.item.complete">查看</a>
- <a v-else > 修改</a>
- </template>
- </data-show>
- </template >
五, Vue 的错误捕获
全局配置 errorHandler
从 2.2.0 起, 这个钩子也会捕获组件生命周期钩子里的错误.
从 2.4.0 起这个钩子也会捕获 Vue 自定义事件处理函数内部的错误了.
更详细的说明可以查看文档 errorHandler
生命周期钩子 errorCaptured
2.5.0 + 新增
更详细的说明可以查看文档 errorCaptured
如果熟悉 React 的话, 会发现它跟错误边界 (Error Boundaries) 的概念很像, 实际上也确实是这么用的.
在文档 Error Handling with errorCaptured Hook 就举了一个典型的例子
- Vue.component('ErrorBoundary', {
- data: () => ({ error: null }),
- errorCaptured (err, vm, info) {
- this.error = `${err.stack}\n\nfound in ${info} of component`
- return false
- },
- render (h) {
- if (this.error) {
- return h('pre', { style: { color: 'red' }}, this.error)
- }
- // ignoring edge cases for the sake of demonstration
- return this.$slots.default[0]
- }
- })
- <error-boundary>
- <another-component/>
- </error-boundary >
需要强调的是 errorCaptured 并不能捕获自身错误和异步错误(比如网络请求, 鼠标事件等产生的错误).
In 2.5 we introduce the new errorCaptured hook. A component with this hook captures all errors (excluding those fired in async callbacks) from its child component tree (excluding itself).
参考
- https://github.com/vuejs/vue/releases
- https://github.com/vuejs/vue-loader/releases/
- https://github.com/less/less.js/releases/
来源: https://juejin.im/post/5c204c98e51d454637699e33