This article mainly introduces the WeChat mini program development tutorial on adding mixin extensions. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
Mixin is a kind of thought. Use partially implemented interfaces to achieve code reuse. It can be used to solve the problem of multiple inheritance and can be used to extend functions. The following article mainly introduces you to the relevant information about adding mixin extensions to WeChat mini programs. Friends who need it can refer to it. Let’s take a look together.
Introduction to Mixin
Mixin (weaving) mode is not one of GOF’s “Design Patterns” summary, but it is used in various Languages and frameworks will find some application of this pattern (or idea). Simply put, Mixin is an interface with full or partial implementation, and its main function is better code reuse.
The concept of Mixin is supported in React and Vue. It provides convenience for us to abstract business logic and code reuse. However, the native mini program framework does not directly support Mixin. Let’s first look at a very practical requirement:
Add a running environment class to all mini program pages to facilitate some style hacks. Specifically, when the mini program is run in different operating environments (Developer Tools|iOS|Android), the platform value is the corresponding operating environment value ("ios|android|devtools")
1 2 3 |
|
Reviewing the use of mixin in vue
The problems mentioned at the beginning of the article are very suitable to be solved using Mixin. We converted this requirement into a Vue problem: add a platform style class to each routing page (although this may not be practical). The implementation idea is to add a data: platform
to each routing component. The code is implemented as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 |
|
In this way, the viewModel of the index and detail routing pages has the value platform, which can be used directly in the template.
mixin classification in vue
data mixin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
vue Mixin merging and execution strategy If there are duplications between mixins, these mixins will have specific merging and execution strategies. As shown below:
How to make the applet support mixinIn front, we reviewed vue Knowledge about mixins. Now we need to make the mini program also support mixin and realize the same mixin function in vue.
Implementation ideasLet’s first take a look at the official mini program page registration method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
If we add mixin configuration, the above official registration method It will become:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
There are two points here, we should pay special attention to:
- Configure the data, method, lifecycle, etc. of the mini program page through configObj
. In fact, Page(configObj)
is an ordinary function call. We add an intermediate method: <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>Page(createPage(configObj))</pre><div class="contentsignin">Copy after login</div></div><div class="contentsignin">Copy after login</div></div>
In the createPage method, we can preprocess the mixin in configObj and put the configuration in it. Merge it into configObj in the correct way, and finally hand it over to
. This is the idea of implementing mixins.
Specific implementationThe specific code implementation will not be described in detail. You can see the following code. For more detailed code implementation, more extensions, and tests, please refer to github
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
1. This article mainly talks about how to add mixin support to small programs . The implementation idea is: preprocessing configObj
2. When dealing with mixin duplication, be consistent with vue:
data, methods will be merged, the component itself has the highest priority, followed by mixins The configured mixin has a higher priority.
lifecycle will not be merged. First execute the lifecycle in the mixins sequentially, and then execute the lifecycle of the component itself.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Two methods to jump to pages in WeChat mini programsJS implementation of tabs in WeChat development Tab effectThe above is the detailed content of WeChat Mini Program Development Tutorial: Adding Mixin Extensions. For more information, please follow other related articles on the PHP Chinese website!