当前位置:网站首页>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 :
-
toggleThe 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 .
版权声明
本文为[:::::::]所创,转载请带上原文链接,感谢
边栏推荐
- The choice of enterprise database is usually decided by the system architect - the newstack
- Wiremock: a powerful tool for API testing
- 使用 Iceberg on Kubernetes 打造新一代云原生数据湖
- Face to face Manual Chapter 16: explanation and implementation of fair lock of code peasant association lock and reentrantlock
- 6.6.1 localeresolver internationalization parser (1) (in-depth analysis of SSM and project practice)
- Linked blocking Queue Analysis of blocking queue
- Windows 10 tensorflow (2) regression analysis of principles, deep learning framework (gradient descent method to solve regression parameters)
- 100元扫货阿里云是怎样的体验?
- What is the difference between data scientists and machine learning engineers? - kdnuggets
- [event center azure event hub] interpretation of error information found in event hub logs
猜你喜欢

Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection

关于Kubernetes 与 OAM 构建统一、标准化的应用管理平台知识!(附网盘链接)

Using Es5 to realize the class of ES6

This article will introduce you to jest unit test

大数据应用的重要性体现在方方面面

一篇文章带你了解SVG 渐变知识

How long does it take you to work out an object-oriented programming interview question from Ali school?
![[C / C + + 1] clion configuration and running C language](/img/5b/ba96ff4447b150f50560e5d47cb8d1.jpg)
[C / C + + 1] clion configuration and running C language

“颜值经济”的野望:华熙生物净利率六连降,收购案遭上交所问询

一篇文章带你了解CSS3圆角知识
随机推荐
Just now, I popularized two unique skills of login to Xuemei
Classical dynamic programming: complete knapsack problem
Face to face Manual Chapter 16: explanation and implementation of fair lock of code peasant association lock and reentrantlock
Wiremock: a powerful tool for API testing
Every day we say we need to do performance optimization. What are we optimizing?
小程序入门到精通(二):了解小程序开发4个重要文件
6.1.1 handlermapping mapping processor (1) (in-depth analysis of SSM and project practice)
PHP应用对接Justswap专用开发包【JustSwap.PHP】
容联完成1.25亿美元F轮融资
Polkadot series (2) -- detailed explanation of mixed consensus
如何玩转sortablejs-vuedraggable实现表单嵌套拖拽功能
Windows 10 tensorflow (2) regression analysis of principles, deep learning framework (gradient descent method to solve regression parameters)
关于Kubernetes 与 OAM 构建统一、标准化的应用管理平台知识!(附网盘链接)
6.6.1 localeresolver internationalization parser (1) (in-depth analysis of SSM and project practice)
I think it is necessary to write a general idempotent component
(2)ASP.NET Core3.1 Ocelot路由
Brief introduction of TF flags
In order to save money, I learned PHP in one day!
Filecoin最新动态 完成重大升级 已实现四大项目进展!
基於MVC的RESTFul風格API實戰