Home>Article>Web Front-end> How to elegantly encapsulate third-party components in Vue? Introduction to packaging methods
How to elegantly encapsulate third-party components in vue? The following article will use examples to explore ways to more elegantly encapsulate third-party components in Vue. I hope it will be helpful to everyone!
During actual development, in order to reduce reinvention, improve work efficiency, and save development time and cost, It is inevitable to use UI component libraries, such as element-ui, which is very popular in web front-ends. But sometimes, we need to make some modifications based on the original component, such as an image component. We need to unify the specific image displayed when the image loading fails, and add it every time the component is used. It is troublesome and time-consuming. The key is the high maintenance cost. When you need to update the image that has been loaded incorrectly, You have to find and modify the places where this component is used again. (Learning video sharing:vuejs tutorial)
Another example is custom paging components, which are also very common. The style of the component and the number of pages per page supported by default. Encapsulation and reuse are beneficial in terms of maintainability and development efficiency.
However, it is not necessary to write a paging component from scratch, just encapsulate it on the original basis. So how to quickly and elegantly encapsulate the components of a third-party component library? v-bind=" Encapsulate el-image as a custom-image component. All places where custom-image is used to display images. During the image loading process, the "Loading..." prompt will be displayed uniformly, and when a loading error occurs, a unified default image will be displayed. The following is a custom-image component loading process and the effect of loading errors.
2. Introduction to key technical points
Contains attributes that are not recognized (and obtained) as props in the parent scope
Binding (class and style except). When a component does not declare any prop, This will contain all parent scope bindings (except class and style), and can be passed through v-bind="$attrs" Import internal components - useful when creating high-level components.For example, after using v-bind="$attrs" for our encapsulated custom-Image component, We also have almost all the properties of el-image in the custom-Image component. And its effect and usage are the same as when we use el-image. In other words, we can use custom-Image by looking at el-image's article.
2.v-on="$listeners"
v-on=" l ##l
It can be passed into internal components via v-on="$listeners" - very useful when creating higher level components.
An introduction taken from the vue official website
Examples of using custom-Image components
Example of encapsulating el-image into a custom-Image component
加载中...
(Learning video sharing:web front-end development,Introduction to programming)
The above is the detailed content of How to elegantly encapsulate third-party components in Vue? Introduction to packaging methods. For more information, please follow other related articles on the PHP Chinese website!