当前位置:网站首页>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 .
版权声明
本文为[:::::::]所创,转载请带上原文链接,感谢
边栏推荐
- 小程序入门到精通(二):了解小程序开发4个重要文件
- Real time data synchronization scheme based on Flink SQL CDC
- 5.4 static resource mapping
- Group count - word length
- 6.3 handlerexceptionresolver exception handling (in-depth analysis of SSM and project practice)
- Python自动化测试学习哪些知识?
- 6.2 handleradapter adapter processor (in-depth analysis of SSM and project practice)
- 容联完成1.25亿美元F轮融资
- (2)ASP.NET Core3.1 Ocelot路由
- Synchronous configuration from git to consult with git 2consul
猜你喜欢

Just now, I popularized two unique skills of login to Xuemei

小程序入门到精通(二):了解小程序开发4个重要文件
![[C / C + + 1] clion configuration and running C language](/img/5b/ba96ff4447b150f50560e5d47cb8d1.jpg)
[C / C + + 1] clion configuration and running C language

Grouping operation aligned with specified datum

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

Thoughts on interview of Ali CCO project team

從小公司進入大廠,我都做對了哪些事?

从海外进军中国,Rancher要执容器云市场牛耳 | 爱分析调研

Troubleshooting and summary of JVM Metaspace memory overflow

Tool class under JUC package, its name is locksupport! Did you make it?
随机推荐
CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识
Summary of common algorithms of linked list
(2)ASP.NET Core3.1 Ocelot路由
前端都应懂的入门基础-github基础
Save the file directly to Google drive and download it back ten times faster
Synchronous configuration from git to consult with git 2consul
IPFS/Filecoin合法性:保护个人隐私不被泄露
Brief introduction and advantages and disadvantages of deepwalk model
教你轻松搞懂vue-codemirror的基本用法:主要实现代码编辑、验证提示、代码格式化
一篇文章带你了解CSS对齐方式
2019年的一个小目标,成为csdn的博客专家,纪念一下
Leetcode's ransom letter
Not long after graduation, he earned 20000 yuan from private work!
Natural language processing - BM25 commonly used in search
vue任意关系组件通信与跨组件监听状态 vue-communication
ES6学习笔记(四):教你轻松搞懂ES6的新增语法
Natural language processing - wrong word recognition (based on Python) kenlm, pycorrector
人工智能学什么课程?它将替代人类工作?
Using consult to realize service discovery: instance ID customization
Flink的DataSource三部曲之二:内置connector