Home>Article>Web Front-end> What are the differences between vue and WeChat mini programs?

What are the differences between vue and WeChat mini programs?

青灯夜游
青灯夜游 Original
2022-01-04 11:04:38 2608browse

Difference: 1. "v-if" and "v-show" are used in vue to control the display and hiding of elements, while "wx-if" and "hidden" are used in mini programs; 2. vue Use "v-on:event" to bind events, while the mini program uses "bindtap(bind event)" to bind events.

What are the differences between vue and WeChat mini programs?

The operating environment of this tutorial: windows7 system, vue2.9.6 version, DELL G3 computer.

1. Life cycle

First post two pictures:

vue life cycle

mini program life cycle

#In comparison, the hook function ofmini programis much simpler. The hook function of

vuewill be triggered when jumping to a new page, but the hook function of小programwill be triggered in different jump methods of the page. Hooks are not the same.

  • onLoad: Page loading
    A page will only be called once. You can get thequery called to open the current page inonLoadParameters.
  • onShow: Page display
    will be called every time the page is opened.
  • onReady: The initial rendering of the page is completed
    A page will only be called once, which means that the page is ready and can interact with the view layer.
    Please set interface settings such aswx.setNavigationBarTitleafteronReady. For details, see Life Cycle
  • onHide: Page Hide
    Called whennavigateToor the bottom tab is switched.
  • onUnload: Page unloading
    Called whenredirectToornavigateBack.

Data request

When the page is loaded to request data, the use of the two hooks is somewhat similar,vuewill generally be in ## Data is requested in #createdormounted, while inminiprogram, data will be requested inonLoadoronShow.

2. Data Binding

VUE: When Vue dynamically binds a variable whose value is an attribute of an element, it will add a colon in front of the variable: , Example:

小program: When the value of a variable is bound to an element attribute, it will be enclosed in two curly brackets. If there are no brackets, it will be considered a character. string. Example:

3. List rendering

Post the code directly, the two are still somewhat similar

vue:

  • {{ item.message }}
var example1 = new Vue({ el: '#example-1', data: { items: [ { message: 'Foo' }, { message: 'Bar' } ] } })

Mini program:

Page({ data: { items: [ { message: 'Foo' }, { message: 'Bar' } ] } }){{item}}

4. Show and hide elements

vue, usev-ifandv-showcontrol the display and hiding of elements

In applet, usewx-ifandhiddenControl the display and hiding of elements

5. Event processing

vue: Usev-on:eventBind the event, or use@eventto bind the event, for example:

  //阻止事件冒泡

In the applet, usebindtap(bind event), orcatchtap(catch event)binding event, for example:

  //阻止事件冒泡

6. Two-way data binding

##1. Setting When the value

is in

vue

, you only need to addv-modelto theformelement, and then bind ## A corresponding value in #data, when the content of the form element changes, the corresponding value indatawill also change accordingly. This is a very nice point invue.

new Vue({ el: '#app', data: { reason:'' } })
But in小program

, there is no such function. then what should we do?

When the content of the form changes, the method bound to the form element will be triggered, and then in this method, the value on the form will be assigned throughthis.setData({key:value})Give the corresponding value in
data.The following is the code, you can feel it:

<input bindinput="bindReason" placeholder="填写理由" class=&#39;reason&#39; value=&#39;{{reason}}&#39; name="reason" /> Page({ data:{ reason:&#39;&#39; }, bindReason(e) { this.setData({ reason: e.detail.value }) } })
When there are many form elements on the page, changing the value is a physical job. Compared with
小program

,

vue’sv-modelis so cool.2. In the value

vue

, use

this.reasonto get the valueIn the applet

, the value is obtained through

this.data.reason7. Binding event parameters passing

in# In ##vue, binding event parameters is quite simple. You only need to pass in the data to be passed as formal parameters in the method that triggers the event, for example:

 new Vue({ el: '#app', methods:{ say(arg){ consloe.log(arg) } } })

InIn the applet, parameters cannot be passed directly in the method of binding events. The parameters need to be used as attribute values, bound to the

data-

attribute on the element, and then in the method , obtained throughe.currentTarget.dataset.*, so as to complete the transfer of parameters. It is very troublesome. Is there any...

<view class=&#39;tr&#39; bindtap=&#39;toApprove&#39; data-id="{{item.id}}"></view> Page({ data:{ reason:&#39;&#39; }, toApprove(e) { let id = e.currentTarget.dataset.id; } })

八、父子组件通信

1.子组件的使用

vue中,需要:

  • 编写子组件

  • 在需要使用的父组件中通过import引入

  • vuecomponents中注册

  • 在模板中使用

//子组件 bar.vue   // 父组件 foo.vue  

小程序中,需要:

  • 编写子组件

  • 在子组件的json文件中,将该文件声明为组件

    { "component": true }
  • 在需要引入的父组件的json文件中,在usingComponents填写引入组件的组件名以及路径

    "usingComponents": { "tab-bar": "../../components/tabBar/tabBar" }
  • 在父组件中,直接引入即可

    具体代码:

    // 子组件     首页    设置  

2.父子组件间通信

vue

父组件向子组件传递数据,只需要在子组件通过v-bind传入一个值,在子组件中,通过props接收,即可完成数据的传递,示例:

// 父组件 foo.vue   // 子组件bar.vue  

子组件和父组件通信可以通过this.$emit将方法和数据传递给父组件。

小程序

父组件向子组件通信和vue类似,但是小程序没有通过v-bind,而是直接将值赋值给一个变量,如下:

 此处, “index”就是要向子组件传递的值

在子组件properties中,接收传递的值

properties: { // 弹窗标题 currentpage: { // 属性名 type: String, // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型) value: 'index' // 属性初始值(可选),如果未指定则会根据类型选择一个 } }

子组件向父组件通信和vue也很类似,代码如下:

//子组件中 methods: { // 传递给父组件 cancelBut: function (e) { var that = this; var myEventDetail = { pickerShow: false, type: 'cancel' } // detail对象,提供给事件监听函数 this.triggerEvent('myevent', myEventDetail) //myevent自定义名称事件,父组件中使用 }, } //父组件中  // 获取子组件信息 toggleToast(e){ console.log(e.detail) }

如果父组件想要调用子组件的方法

vue会给子组件添加一个ref属性,通过this.$refs.ref的值便可以获取到该子组件,然后便可以调用子组件中的任意方法,例如:

//子组件  //父组件 this.$ref.bar.子组件的方法

小程序是给子组件添加id或者class,然后通过this.selectComponent找到子组件,然后再调用子组件的方法,示例:

//子组件  // 父组件 this.selectComponent('#id').syaHello()

【相关推荐:《vue.js教程》】

The above is the detailed content of What are the differences between vue and WeChat mini programs?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn