After writing a Vue project and a mini program, I found that the two have many similarities. Here I would like to summarize the similarities and differences between the two. 1. Life Cycle First post two pictures: Vue life cycle Mini Program Life Cycle In comparison, the hook function of the applet is much simpler. The hook function of vue will be triggered when jumping to a new page, but the hook function of the mini program will trigger different hooks depending on the page jump method.
A page will only be called once, and the query parameters called to open the current page can be obtained in onLoad.
This function is called every time a page is opened.
A page is called only once, indicating that the page is ready to interact with the view layer. Please set the interface settings such as wx.setNavigationBarTitle after onReady. See Life Cycle for details.
Called when navigateTo or bottom tab is switched.
Called when redirectTo or navigateBack. Data Request When requesting data when loading a page, the usage of the two hooks is somewhat similar. Vue generally requests data in create or mounted, while in mini programs, it requests data in onLoad or onShow. 2. Data Binding When VUE dynamically binds the value of a variable to an attribute of an element, a colon is added in front of the variable:, for example:
Mini Program: When binding a variable value to an element attribute, it will be enclosed in two curly brackets. If no brackets are added, it will be considered a string. Example:
3. List Rendering Paste the code directly, the two are still somewhat similar vue:
Mini Programs:
4. Showing and hiding elements In vue, use v-if and v-show to control the display and hiding of elements In the applet, use wx-if and hidden to control the display and hiding of elements 5. Event Handling Vue: Use v-on:event to bind events, or use @event to bind events, for example:
In the applet, all events are bound using bindtap (bind+event) or catchtap (catch+event), for example:
6. Two-way data binding 1. Setting Values In Vue, you only need to add v-model to the form element, and then bind a corresponding value in the data. When the content of the form element changes, the corresponding value in the data will also change accordingly. This is a very nice point of Vue.
But there is no such function in the mini program. What should I do? When the form content changes, the method bound to the form element will be triggered, and then in this method, the value on the form is assigned to the corresponding value in data through this.setData({key:value}). Here is the code to give you a feel for it:
When there are many form elements on the page, changing the value is a physical job. Compared with the applet, vue's v-model is so cool. 2. Value In Vue, get the value through this.reason In the applet, get the value through this.data.reason 7. Binding event parameters In Vue, it is very simple to bind events and pass parameters. You only need to pass the data you want to pass as a parameter in the method that triggers the event. For example:
In the applet, you cannot pass parameters directly into the method of binding events. You need to bind the parameters as attribute values to the data-attribute on the element, and then obtain them in the method through e.currentTarget.dataset.* to complete the parameter transfer. Is it troublesome?
8. Parent-child component communication 1. Use of subcomponents In Vue, you need to:
In the applet, you need to: 1. Write subcomponents 2. In the json file of the subcomponent, declare the file as a component
3. In the json file of the parent component that needs to be introduced, fill in the component name and path of the imported component in usingComponents
4. In the parent component, just import it directly
Specific code:
2. Communication between parent and child components In Vue The parent component passes data to the child component. It only needs to pass a value through v-bind in the child component. In the child component, the data can be received through props. Example:
Child components and parent components can communicate by passing methods and data to the parent component through this.$emit. In the applet The communication between the parent component and the child component is similar to Vue, but the mini program does not use v-bind, but directly assigns the value to a variable, as follows:
Here, "index" is the value to be passed to the child component In the child component properties, receive the passed value
The communication between the child component and the parent component is also very similar to Vue. The code is as follows:
If the parent component wants to call the method of the child component Vue will add a ref attribute to the subcomponent. The subcomponent can be obtained through the value of this.$refs.ref, and then any method in the subcomponent can be called, for example:
The mini program adds an id or class to the subcomponent, then finds the subcomponent through this.selectComponent, and then calls the method of the subcomponent. For example:
Mini Programs and Vue are very similar in this respect, aren’t they? 9. Nonsense There are still many places that I haven't written, I will slowly add and simplify them later. I feel that what I wrote is a bit redundant, please don't criticize me!!! If you find it helpful, please like and collect it. |
<<: How to update the iOS 12 beta version via mobile OTA
>>: Use this free Sketch plugin to help you perfectly restore the Android interface!
Last week I talked with you about data constructi...
Bidding itself is a place to spend money. If the ...
As the proposer of the user pyramid model, Lei Le...
Sogou Promotion PC Multi-Picture Promotion Style ...
Since Keep was launched in February 2015, it now ...
Produced by: Science Popularization China Author:...
Cross-border marketing is the process of combinin...
Have you ever encountered such accounts in the pr...
On March 16, following the launch of QPR2, Google...
According to the latest research report released b...
How much does it cost to join a fresh food mini p...
Weight is an important indicator for evaluating h...
I once heard this saying: "Sweat is the tear...
Not long ago, the news about Xiaohongshu conducti...
The "Earth Hour" event is a global even...