当前位置:网站首页>Advanced Vue component pattern (3)
Advanced Vue component pattern (3)
2020-11-06 01:23:00 【:::::::】
03 Use mixin To enhance Vue Components
The goal is
In the previous article , Although we will toggle
Components are divided into toggle-button
、toggle-on
and toggle-off
Three sub components , And everything works well , But there are some problems in it :
-
toggle
The internal state and method of a component can only be shared with these three subcomponents , We expect that third-party components can also share these States and methods - inject We wrote the injection logic three times , If you can , We would prefer to declare only once (DRY principle )
- inject The injection logic of is currently hard coded , In some cases , We may expect dynamic configuration
If you are familiar with react The reader here may immediately think of HOC( High order component ) The concept of , And it's also react One of the most common patterns , This mode can improve react The degree of reuse and flexibility of components . stay vue in , Do we also have some means or features to improve the reusability and flexibility of components ? The answer, of course, is yes , That's it mixin.
Realization
About mixin Own knowledge , I won't go over it here , Unfamiliar readers can go to official documents to understand . We have made a declaration called toggleMixin Of mixin To pull out the public injection logic , as follows :
export const withToggleMixin = { inject: { toggleComp: "toggleComp" } };
after , Whenever you need to inject toggle
When a component provides a dependency , Just blend into the present mixin, as follows :
mixins: [withToggleMixin]
If the logic of Injection , Let's add some flexibility , For example, you want to freely declare that you want to inject dependencies key when , We can borrow from HOC The concept of , Declare a higher order mixin( Can be abbreviated HOM ?? A skin , Very happy ), as follows :
export function withToggle(parentCompName = "toggleComp") { return { inject: { [parentCompName]: "toggleComp" } }; }
This HOC mixin It can be used as follows :
mixins: [withToggle("toggle")]
So in the current component , call toggle Component related states and methods , It is no longer this.toggleComp
, It is this.toggle
.
results
By implementing toggleMixin, We managed to pull the logic out of the injection , So every time you need to share toggle
When the state and method of the component , Mix in the mixin that will do . This solves the problem that third-party components cannot share their states and methods , In the online instance code , I implemented two third-party components , Namely custom-button
and custom-status-indicator
, The former is a custom switch , Use withToggleMixin To mix in the injection logic , The latter is a custom status indicator , Use withToggle Higher order functions to mix in injection logic .
You can see the implementation code and demonstration of this component in the following link :
- sandbox: The online demo
- github: part-3
summary
mixin As a distribution Vue A very flexible way to reuse functions in components , It can be used in many scenes , Especially in some components dealing with public logic , For example, notice 、 Form error prompt, etc , This mode is especially useful .
Catalog
github gist Click preview
Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .
版权声明
本文为[:::::::]所创,转载请带上原文链接,感谢
边栏推荐
- Grouping operation aligned with specified datum
- Arrangement of basic knowledge points
- Skywalking series blog 2-skywalking using
- [JMeter] two ways to realize interface Association: regular representation extractor and JSON extractor
- git rebase的時候捅婁子了,怎麼辦?線上等……
- How do the general bottom buried points do?
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- ES6 essence:
- 基於MVC的RESTFul風格API實戰
- 6.4 viewresolver view parser (in-depth analysis of SSM and project practice)
猜你喜欢
Not long after graduation, he earned 20000 yuan from private work!
CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识
Existence judgment in structured data
ES6学习笔记(二):教你玩转类的继承和类的对象
Architecture article collection
Flink的DataSource三部曲之二:内置connector
一篇文章带你了解CSS3圆角知识
git rebase的時候捅婁子了,怎麼辦?線上等……
Want to do read-write separation, give you some small experience
Tool class under JUC package, its name is locksupport! Did you make it?
随机推荐
一篇文章带你了解CSS3图片边框
6.4 viewresolver view parser (in-depth analysis of SSM and project practice)
Let the front-end siege division develop independently from the back-end: Mock.js
Every day we say we need to do performance optimization. What are we optimizing?
Summary of common algorithms of binary tree
vue-codemirror基本用法:实现搜索功能、代码折叠功能、获取编辑器值及时验证
Group count - word length
100元扫货阿里云是怎样的体验?
快快使用ModelArts,零基礎小白也能玩轉AI!
JVM memory area and garbage collection
6.5 request to view name translator (in-depth analysis of SSM and project practice)
What problems can clean architecture solve? - jbogard
High availability cluster deployment of jumpserver: (6) deployment of SSH agent module Koko and implementation of system service management
每个前端工程师都应该懂的前端性能优化总结:
5.4 static resource mapping
合约交易系统开发|智能合约交易平台搭建
PHP应用对接Justswap专用开发包【JustSwap.PHP】
Python crawler actual combat details: crawling home of pictures
How do the general bottom buried points do?
中小微企业选择共享办公室怎么样?