Home >Web Front-end >JS Tutorial >Detailed analysis of several easily overlooked parts of Vue documentation
In view of some contents in the Vue document that you may not study, this article mainly introduces the detailed analysis of several easily overlooked parts of the Vue document. It is of great practical value. Friends who need it can refer to it
Regarding some of the contents in the Vue documentation that you may not read, I made a small summary. As a quick snack for experienced people, it is not particularly suitable for beginners and may have some flaws. I hope you can give me more suggestions.
Code-saving mixin
mixin concept: component-level reusable logic, including data variables/life cycle hooks/public methods, thus mixing in It can be used directly in components without having to rewrite redundant logic (similar to inheritance)
Usage:
Create a mixin folder under a certain public folder pub, and under it Create mixinTest.js
const mixinTest = { created() { console.log(`components ${this.name} created`) }, methods: { hello() { console.log('mixin method hello') } } } export default mixinTest
Reference the public mixin file just now in the component and use
import mixinTest from '../pub/mixin/mixinTest.js' export default { data() { return { name: 'hello' } }, mixins: [mixinTest], methods: { useMixin() { this.hello() } } }
ps: If you use the Vue.mixin() method, it will affect all Vue examples created later, so use with caution!
Pay attention to several characteristics of mixin:
The data variables mixed in are shallow merges. In case of conflicts, the data in the component takes precedence (custom variables in the object)
The logic in the mixed life cycle function will be merged with the life cycle function logic defined in the component, and executed first (created/mounted/destroy)
The mixed value is an object option, which will be mixed into an object. After a conflict, the key name within the component will take precedence (data/method/components/directives)
slot content distribution
Introduction to the concept of slot: The difference between Vue and React in writing lies in the organization of internal elements of components and sub-components. There is no children element in the component for us to access and Display (disregarding the render function for now), the API instead is slot
Usage scenario definition:
Customized sub-component with nested HTML inside Or other custom label components
This custom sub-component is written in the parent component, and nested things are also placed in the parent component
By using the 58cb293b8600657fad49ec2c8d37b4727971cf77a46923278913ee247bc958ee tag in the template of the child component, the effect of rendering the nested tags written in the parent component is achieved
Essence It is to put the content of the parent component in the child component and insert it into the position of the child component. Multiple tags will also be inserted together
<template> <p id="app"> <self-component> <!--self-component表示自定义的组件--> <span>12345</span> <!--父组件里的嵌套标签--> </self-component> </p> </template> <script> export default { components: [selfComponent] } </script> <!--self-component的组件模板--> <template> <p> <button><slot></slot></button> </p> </template> <script> export default { // 只有子组件的模板里面有slot标签,才能取到写在自定义组件里面的标签的渲染引用 } </script>
Two advanced points of the slot feature:
The compilation scope of slot inserted content: the scope of the distributed content is determined according to the template where it is located
Specific The location where the content is written determines the scope of compilation (in most cases under the scope of the parent component)
2.1.0 Added a new scope slot, so that the child component can be The properties of the component are exposed to the content written in the child component in the parent component. Use the slot tag in the child component to write custom properties directly, and then the parent component writes the tag in the slot. Add the slot-scope attribute
<!-- 父组件模板 --> <child :items="items"> <!-- 作用域插槽也可以是具名的 --> <li slot="item" slot-scope="props" class="my-fancy-item">{{ props.text }}</li> </child> <!-- 子组件模板 --> <ul> <slot name="item" v-for="item in items" :text="item.text"> <!-- 这里写当父组件引用子组件但没写内部内容时展示的东东 --> </slot> </ul>
slot's name attribute to specify the location where the label is inserted, which is the named slot in the document (This official document makes it clear)
The slot written in the template of the subcomponent has a name attribute (af84d27f87c182823f3fa1c1661899d27971cf77a46923278913ee247bc958ee)
Write the slot content in the child component in the parent component and specify the slot attribute (51b5f07300a0fda9b389018dc635da2c12394b3e26ee717c64999d7867364b1b4a3)
The content of the parent component will be placed in the correct location corresponding to slot==name
If the slot attribute is not specified, it will be placed in the anonymous slot by default. On
Dynamic component has this feature. Many people have written many Vue projects, but they have not used it. Having said this, it is necessary to say a few more words
Applicability of dynamic components:
Single-page application, the switching of some components does not involve routing, but only a certain area on the page Components need to be changed
The definitions of the changed component parameters are consistent. For example, they are all dialog boxes and an object needs to be passed in, but the data structure in the object is different
By using the component's is attribute, we avoid redundant component code in the template and avoid multiple v-if template codes to be cleaner.
The method used (reference Document):
<keep-alive> <component v-bind:is="currentView"> <!-- 组件在 vm.currentview (对应组件名称)变化时改变! --> <!-- 非活动组件将被缓存!可以保留它的状态或避免重新渲染 --> </component> </keep-alive>
Note:
dynamically switched components must be introduced into the parent component and rendered is dynamic, but imports are not.
7c9485ff8c3cba5ae9343ed63c2dc3f7When wrapping dynamic components, inactive component instances will be cached to improve performance and avoid repeated rendering (keep-alive will not render additional DOM structures)
7c9485ff8c3cba5ae9343ed63c2dc3f7There are two attributes, include and exclude, which are used to specify cached and non-cached components (pass in string/array/regular)
Another way to avoid re-rendering is to add the attribute v-once to the tag, which is used to cache a large amount of static content and avoid repeated rendering.
ps:7c9485ff8c3cba5ae9343ed63c2dc3f7不会在函数式组件中正常工作,因为它们没有缓存实例。
动画与过渡
其实很多前端工程师第一次用Vue的动画和过渡都是通过库组件来做到的,所以对这块没怎么深挖,各种过渡特效和按钮动画就跑起来了,现在就看下文档,补补课
前端实现动画的基本方法分为三种种:css3的过渡和keyframe/javascript操纵dom/使用webgl或者canvas来独立实现,其中第三种是作为展示动画,与交互结合较少,而Vue作为一个框架,其支持动画基是从前两种入手的,从官方文档提到的四种支持就可以看出这一点。不过官方文档是从DOM过渡和状态过渡两个方面来讲解,前者是DOM的消失和出现的动画等属性的变化,后者是页面上某些值的变化。
DOM属性的改变
若是单个元素/组件的显隐,在组件外面包裹一层300ff3b250bc578ac201dd5fb34a00046087faffb1c3f26530d25a6b190c2f81,而后选择是css过渡还是javascript过渡
CSS过渡:
vue提供了六个样式后缀,本质是在dom过渡的过程中动态地添加和删除对应的className。(-[enter|leave]-?[active|to]?)
如果用css库来辅助开发,可以在transiton这个标签上定义自定义过渡类名,也是六个属性。([enter|leave]-?[active|to]?-class)
常见的一种效果是元素首次渲染的动画,如懒加载图片飞入,这个时候要在transiton标签上加上appear,另有三个属性可指定(appear-?[to|active]?-class)
<!-- 每种CSS动画库对应的class命名规则可能不同,所以根据不同库要自己写,以animate.css为例 --> <transition name="custom-classes-transition" enter-active-class="animated tada" leave-active-class="animated bounceOutRight" :duration="{ enter: 500, leave: 800 }" >...</transition> <!-- duration属性可以传一个对象,定制进入和移出的持续时间-->
JS过渡:
因为现在很多动画库需要工程师调用库提供的函数,把dom元素传入进行处理,这个时候需要这种方式
通过在transiton这个标签上添加监听事件,共8个([before|after]?-?[enter|leave]-?[cancelled]?)
监听事件的回调函数的第一个参数都是el,为过渡的dom元素,在enter和leave这两个还会传入done作为第二个参数
元素首次渲染的动画,可以指定的监听事件有4个([before|after]?-?appear和appear-cancelled)
<template> <transition v-bind:css="false" v-on:before-enter="beforeEnter" v-on:enter="enter" v-on:leave="leave" v-on:leave-cancelled="leaveCancelled"> <!-- 对于仅使用 JavaScript 过渡的元素添加 v-bind:css="false",Vue 会跳过 CSS 的检测 --> </transition> </template> <script> methods: { // 以Velocity库为例 beforeEnter: function (el) {/*...*/}, // 此回调函数是可选项的设置 enter: function (el, done) { // Velocity(el, { opacity: 1, fontSize: '1.4em' }, { duration: 300 }) done() //回调函数 done 是必须的。否则,它们会被同步调用。 }, leave: function (el, done) { // Velocity(el, { translateX: '15px', rotateZ: '50deg' }, { duration: 600 }) done() }, leaveCancelled: function (el) {/*...*/} } </script>
多元素过渡其实就是一句话:照常使用v-if/v-else的同时对同一种标签加上key来标识
Vue对于这种多元素动画有队列上的处理,这就是transiton这个标签上的mode属性,通过指定(in-out|out-in)模式,实现消失和出现动画的队列效果,让动画更加自然。
<transition name="fade" mode="out-in"> <!-- ... the buttons ... --> </transition>
多组件过渡也是一句话:用上一节提到的动态组件,即可完成。
针对列表过渡,其本质仍是多个元素的同时过渡,不过列表大部分是通过数组动态渲染的,因此有独特的地方,不过整体的动画思路不变。具体有以下几点
使用transitoin-group这个组件,其需要渲染为一个真实元素,可以通过tag这个属性来指定。
列表的每个元素需要提供key属性
使用CSS过渡的话,要考虑到列表内容变化过程中,存在相关元素的定位改变,如果要让定位是平滑过渡的动画,要另外一个v-move属性。 这个属性是通过设置一个css类的样式,来将创建元素在定位变化时的过渡,Vue内部是通过FLIP实现了一个动画队列,只要注意一点就是过渡元素不能设置为display:inline,这里需要文档上的代码做一个简短的demo:(其实通过在li上设置过渡transition属性也可以实现v-move的效果)
<template> <button v-on:click="shuffle">Shuffle</button> <transition-group name="flip-list" tag="ul"> <li v-for="item in items" v-bind:key="item">{{ item }}</li> </transition-group> </template> <script> import _ from 'lodash'; export default { data() { return { items: [1,2,3,4,5,6,7,8,9] } }, methods: { shuffle: function () { this.items = _.shuffle(this.items) } } } </script> <style lang="css"> .flip-list-move { transition: transform 1s; } </style>
数值和属性动态变化
这一部分的动画主要是针对数据元素本身的特效,比如数字的增减,颜色的过渡过程控制,svg动画的实现等,其本质都是数字/文本的变化。 我自己总结就是:通过利用Vue的响应式系统,把数字的变化通过外部库把DOM上对应数值的变化做出连续的效果,如1->100是个数字递增的连续过程,黑色->红色的过程。官方文档主要是用几个示例代码来说明,其本质步骤如下:
在页面上通过input的双向绑定修改某一变量a,还有一个处理dom上的过渡效果的变量b
这个数据被watcher绑定(watch对象中某个属性是这个变量a),触发逻辑
在watcher里面的逻辑就是通过外部过渡库,指定初始值b和最终值a,是把b的值最后改为a
DOM上绑定的变量就是b,如果某些复杂情况可能是基于b的计算属性,从而把b的变化过程展现出来
After following the above idea, a unit-level animation effect is completed. This similar process is actually a very common requirement, so it is necessary to encapsulate this process into a component and only expose it. The value to be transitioned serves as the entry point, and every time this value is changed, it is an animated transition effect. Component encapsulation needs to add the initial value specified in the mounted life cycle based on the above four steps. At the same time, the original two values a/b are used as one value in the component, and can be distinguished by newValue and oldValue in the watch object. As for the final SVG, its essence is also a digital transition, but it involves more state variables and longer code. However, there is still not much demand for pure front-end pages, but as a hobby, you can tinker with some fun little ones. demo, but it definitely requires the participation of the designer, otherwise it will be difficult to adjust the parameters.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Angular uses the filter uppercase/lowercase to implement letter case conversion function example
Angular usage operation Example of event command ng-click passing multiple parameters
JavaScript code to implement the upload preview function of txt files
The above is the detailed content of Detailed analysis of several easily overlooked parts of Vue documentation. For more information, please follow other related articles on the PHP Chinese website!