Differences and comparisons between Vue and WeChat applet

Differences and comparisons between Vue and WeChat applet

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.

  • onLoad: page loading

A page will only be called once, and the query parameters called to open the current page can be obtained in onLoad.

  • onShow: page display

This function is called every time a page is opened.

  • onReady: The initial rendering of the page is completed

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.

  • onHide: page hidden

Called when navigateTo or bottom tab is switched.

  • onUnload: page unload

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:

  1. <img :src= "imgSrc" />

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:

  1. <image src= "{{imgSrc}}" ></image>

3. List Rendering

Paste the code directly, the two are still somewhat similar

vue:

  1. <ul id= "example-1" >  
  2. <li v- for = "item in items" >  
  3. {{ item.message }}  
  4. </li>  
  5. </ul>  
  6.  
  7. var example1 = new Vue({  
  8. el: '#example-1' ,  
  9. data: {  
  10. items: [  
  11. { message: 'Foo' },  
  12. { message: 'Bar' }  
  13. ]
  14.   }  
  15. })

Mini Programs:

  1. Page({  
  2. data: {  
  3. items: [  
  4. { message: 'Foo' },  
  5. { message: 'Bar' }  
  6. ]  
  7. }  
  8. })  
  9.  
  10. <text wx: for = "{{items}}" >{{item}}</text>

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:

  1. <button v- on :click= "counter += 1" > Add 1</button>  
  2. <button v- on :click.stop= "counter+=1" >Add1</button> //Prevent event bubbling

In the applet, all events are bound using bindtap (bind+event) or catchtap (catch+event), for example:

  1. <button bindtap= "noWork" >No work tomorrow</button>  
  2. <button catchtap= "noWork" >No work tomorrow</button> //Prevent event bubbling

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.

  1. <div id= "app" >  
  2. <input v-model= "reason" placeholder= "Fill in the reason" class= 'reason' />  
  3. </div>  
  4.  
  5. new Vue({  
  6. el: '#app' ,  
  7. data: {  
  8. reason: ''    
  9. }  
  10. })

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:

  1. <input bindinput= "bindReason" placeholder= "Fill in the reason" class= 'reason' value= '{{reason}}'   name = "reason" />  
  2. Page({  
  3. data:{  
  4. reason: ''    
  5. },  
  6. bindReason(e) {  
  7. this.setData({  
  8. reason: e.detail.value  
  9. })  
  10. }  
  11. })

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:

  1. <button @click= "say('No work tomorrow')" ></button>  
  2. new Vue({  
  3. el: '#app' ,  
  4. methods:{  
  5. say(arg){  
  6. consloe.log(arg)  
  7. }  
  8. }  
  9. })

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?

  1. < view class= 'tr' bindtap= 'toApprove' data-id= "{{item.id}}" ></ view >  
  2. Page({  
  3. data:{  
  4. reason: ''    
  5. },  
  6. toApprove(e) {  
  7. let id = e.currentTarget.dataset.id;  
  8. }  
  9. })

8. Parent-child component communication

1. Use of subcomponents

In Vue, you need to:

  1. Writing subcomponents
  2. Introduce it through import in the parent component where it is needed
  3. Register in Vue's components
  4. Use in templates
  1. //Subcomponent bar.vue  
  2. <template>  
  3. <div class= "search-box" >  
  4. <div @click= "say" :title= "title" class= "icon-dismiss" ></div>  
  5. </div>  
  6. </template>  
  7. <script>  
  8. export default {  
  9. props:{  
  10. title:{  
  11. type:String,  
  12. default : ''    
  13. }  
  14. }  
  15. },
  16.  
  17. methods:{  
  18. say(){
  19.   console.log( 'No work tomorrow' );  
  20. this.$emit( ​​'helloWorld' )  
  21. }  
  22. }
  23. </script>  
  24.  
  25. // Parent component foo.vue  
  26. <template>  
  27. <div class= "container" >  
  28. <bar :title= "title" @helloWorld= "helloWorld" ></bar>  
  29. </div>  
  30. </template>  
  31.  
  32. <script>  
  33. import Bar from   './bar.vue'    
  34. export default {  
  35. data:{  
  36. title: "I am the title"    
  37. },  
  38. methods:{  
  39. helloWorld(){  
  40. console.log( 'I received the event passed by the child component' )  
  41. }  
  42. },  
  43. components:{
  44.   Bar  
  45. }  
  46. </script>

In the applet, you need to:

1. Write subcomponents

2. In the json file of the subcomponent, declare the file as a component

  1. {  
  2. "component" : true    
  3. }

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

  1. "usingComponents" : {  
  2. "tab-bar" : "../../components/tabBar/tabBar"    
  3. }

4. In the parent component, just import it directly

  1. <tab-bar currentpage= "index" ></tab-bar>

Specific code:

  1. // Subcomponent  
  2. <! --components/tabBar/tabBar.wxml-->    
  3. < view class= 'tabbar-wrapper' >  
  4. < view class= 'left-bar {{currentpage==="index"?"active":""}}' bindtap= 'jumpToIndex' >  
  5. <text class= 'iconfont icon-shouye' ></text>  
  6. < view >Home</ view >  
  7. </ view >  
  8. < view class= 'right-bar {{currentpage==="setting"?"active":""}}' bindtap= 'jumpToSetting' >  
  9. <text class= 'iconfont icon-shezhi' ></text>  
  10. < view >Settings</ view >  
  11. </ view >  
  12. </ view >

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:

  1. // Parent component foo.vue  
  2. <template>  
  3. <div class= "container" >  
  4. <bar :title= "title" ></bar>  
  5. </div>  
  6. </template>  
  7. <script>  
  8. import Bar from   './bar.vue'    
  9. export default {  
  10. data:{  
  11. title: "I am the title"    
  12. },  
  13. components:{  
  14. Bar  
  15. }  
  16. </script>  
  17.  
  18. // Subcomponent bar.vue  
  19. <template>  
  20. <div class= "search-box" >  
  21. <div :title= "title" ></div>  
  22. </div>  
  23. </template>  
  24. <script>  
  25. export default {  
  26. props:{  
  27. title:{  
  28. type:String,  
  29. default : ''    
  30. }  
  31. }  
  32. }  
  33. </script>

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:

  1. <tab-bar currentpage= "index" ></tab-bar>

Here, "index" is the value to be passed to the child component

In the child component properties, receive the passed value

  1. properties:  
  2. // Pop-up title  
  3. currentpage: { // property name  
  4. type: String, // Type (required), currently accepted types include: String, Number, Boolean, Object, Array, null (indicates any type)  
  5. value: 'index' // Attribute initial value (optional), if not specified, one will be selected according to the type  
  6. }
  7. }

The communication between the child component and the parent component is also very similar to Vue. The code is as follows:

  1. //In the subcomponent  
  2. methods: {  
  3. // Pass to parent component  
  4. cancelBut: function (e) {  
  5. var that = this;  
  6. var myEventDetail = { pickerShow: false , type: 'cancel' } // detail object, provided to the event listening function  
  7. this.triggerEvent( 'myevent' , myEventDetail) //myevent custom name event, used in parent component  
  8. },
  9. }  
  10. //In the parent component  
  11. <bar bind:myevent= "toggleToast" ></bar>   
  12. // Get subcomponent information  
  13. toggleToast(e){  
  14. console.log(e.detail)  
  15. }

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:

  1. //Subcomponents  
  2. <bar ref= "bar" ></bar>  
  3. // Parent component  
  4. this.$ref.bar.Method of the child component

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:

  1. //Subcomponents  
  2. <bar id= "bar" ></bar>  
  3. // Parent component  
  4. this.selectComponent( '#id' ).syaHello()

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!

Recommend

What is the correct way to open "data analysis"?

Last week I talked with you about data constructi...

How does bidding reduce costs? How to reduce costs with Baidu bidding?

Bidding itself is a place to spend money. If the ...

User pyramid model: Zhihu promotion case analysis

As the proposer of the user pyramid model, Lei Le...

Sogou Promotion PC multi-picture promotion style display!

Sogou Promotion PC Multi-Picture Promotion Style ...

Product Operation丨How to improve Keep’s user stickiness?

Since Keep was launched in February 2015, it now ...

Can black and white color ultrasound still be called color ultrasound?

Produced by: Science Popularization China Author:...

Let’s talk about 5 aspects: How to do brand cross-border marketing?

Cross-border marketing is the process of combinin...

The CTR is the same, which copy has a better conversion rate?

Have you ever encountered such accounts in the pr...

Google Android 13 QPR3 Beta 1 released for Pixel phones

On March 16, following the launch of QPR2, Google...

Have you fallen into the misunderstandings about scientific weight loss?

Weight is an important indicator for evaluating h...

Xiaohongshu promotion tips: How to operate a Xiaohongshu account?

Not long ago, the news about Xiaohongshu conducti...