Home>Article>Web Front-end> What are the differences between vue and WeChat mini programs?
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.
The operating environment of this tutorial: windows7 system, vue2.9.6 version, DELL G3 computer.
First post two pictures:
vue life cycle
mini program life cycle
#In comparison, the hook function ofmini program
is much simpler. The hook function of
vue
will be triggered when jumping to a new page, but the hook function of小program
will be triggered in different jump methods of the page. Hooks are not the same.
onLoad
: Page loadingquery called to open the current page in
onLoad
Parameters.onShow
: Page displayonReady
: The initial rendering of the page is completedwx.setNavigationBarTitle
afteronReady
. For details, see Life CycleonHide
: Page HidenavigateTo
or the bottom tab is switched.onUnload
: Page unloadingredirectTo
ornavigateBack
.Data request
When the page is loaded to request data, the use of the two hooks is somewhat similar,vue
will generally be in ## Data is requested in #createdor
mounted, while in
miniprogram, data will be requested in
onLoador
onShow.
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:
vue:
Mini program:
Page({ data: { items: [ { message: 'Foo' }, { message: 'Bar' } ] } }){{item}}
vue, use
v-ifand
v-showcontrol the display and hiding of elements
In applet, use
wx-ifand
hiddenControl the display and hiding of elements
vue: Use
v-on:eventBind the event, or use
@eventto bind the event, for example:
//阻止事件冒泡
In the applet, use
bindtap(bind event), or
catchtap(catch event)binding event, for example:
//阻止事件冒泡
is in
vue, you only need to addv-model
to theform
element, and then bind ## A corresponding value in #data, when the content of the form element changes, the corresponding value in
datawill also change accordingly. This is a very nice point in
vue.
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:
When there are many form elements on the page, changing the value is a physical job. Compared with<input bindinput="bindReason" placeholder="填写理由" class='reason' value='{{reason}}' name="reason" /> Page({ data:{ reason:'' }, bindReason(e) { this.setData({ reason: e.detail.value }) } })
小program
vue’s
v-modelis so cool.
2. In the value
vue
, usethis.reasonto get the value
In the applet
this.data.reason7. Binding event parameters passing
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
attribute on the element, and then in the method , obtained through 1.子组件的使用 在 编写子组件 在需要使用的父组件中通过 在 在模板中使用 在 编写子组件 在子组件的 在需要引入的父组件的 在父组件中,直接引入即可 具体代码: 2.父子组件间通信 在 父组件向子组件传递数据,只需要在子组件通过 子组件和父组件通信可以通过 在 父组件向子组件通信和 在子组件 子组件向父组件通信和 【相关推荐:《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!e.currentTarget.dataset.*
, so as to complete the transfer of parameters. It is very troublesome. Is there any...<view class='tr' bindtap='toApprove' data-id="{{item.id}}"></view> Page({ data:{ reason:'' }, toApprove(e) { let id = e.currentTarget.dataset.id; } })
八、父子组件通信
vue
中,需要:
import
引入vue
的components
中注册//子组件 bar.vue
小程序
中,需要:
json
文件中,将该文件声明为组件{ "component": true }
json
文件中,在usingComponents
填写引入组件的组件名以及路径"usingComponents": { "tab-bar": "../../components/tabBar/tabBar" }
// 子组件
vue
中v-bind
传入一个值,在子组件中,通过props
接收,即可完成数据的传递,示例:// 父组件 foo.vue
this.$emit
将方法和数据传递给父组件。小程序
中vue
类似,但是小程序
没有通过v-bind
,而是直接将值赋值给一个变量,如下: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自定义名称事件,父组件中使用 }, } //父组件中
如果父组件想要调用子组件的方法
vue
会给子组件添加一个ref
属性,通过this.$refs.ref的值
便可以获取到该子组件,然后便可以调用子组件中的任意方法,例如://子组件
小程序
是给子组件添加id
或者class
,然后通过this.selectComponent
找到子组件,然后再调用子组件的方法,示例://子组件