Vue applets and micro-channel difference

First, the life cycle

First posted two pictures:

vue life cycle

 

Applet lifecycle

 

In contrast, the 小程序hook function is much simpler.

vueJump hook function when a new page, the hook function will be triggered, but 小程序the hook function, different page jump method, hook trigger is not the same.

  • onLoad: Page loading
    a page only called once, you can  onLoad get open the current page in the calling  query parameters.
  • onShow: The page is displayed
    every time you open the page will be called once.
  • onReady: Page rendering is complete for the first time
    a page is only called once, on behalf of the page has been ready, you can interact and view layer.
    Settings interface as wx.setNavigationBarTitlerequested in the onReadyfollowing set. See Life Cycle
  • onHide: Page hide
    when navigateTocalling or bottom of the tab when switching.
  • onUnload: Page unload
    when redirectToor navigateBackwhen called.

Data Request

Page load request data, using both the hook is somewhat similar, vueusually in createdor in mountedthe request data, while 小程序, will onLoador onShowrequest data.

Second, the data binding

VUE: Vue When an attribute value of a variable dynamic binding element, will be added before the variable colon:, Example:

<img :src="imgSrc"/>

 

小程序: The binding properties of elements of the value of a variable, one of two enclosed in braces, brackets, if not, is considered to be a string. example:

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

Third, rendering list

Directly attached to the code, both or some similar
vue:

复制代码
<ul id="example-1">
  <li v-for="item in items">
    {{ item.message }}
  </li>
</ul>

var example1 = new Vue({
  el: '#example-1',
  data: {
    items: [
      { message: 'Foo' },
      { message: 'Bar' }
    ]
  }
})
复制代码

 

小程序:

复制代码
Page({
  data: {
    items: [
      { message: 'Foo' },
      { message: 'Bar' }
    ]
  }
})

<text wx:for="{{items}}">{{item}}</text>
复制代码

四、显示与隐藏元素

vue中,使用v-if 和v-show控制元素的显示和隐藏

小程序中,使用wx-ifhidden控制元素的显示和隐藏

五、事件处理

vue:使用v-on:event绑定事件,或者使用@event绑定事件,例如:

<button v-on:click="counter += 1">Add 1</button>
<button v-on:click.stop="counter+=1">Add1</button>  //阻止事件冒泡

小程序中,全用bindtap(bind+event),或者catchtap(catch+event)绑定事件,例如:

<button bindtap="noWork">明天不上班</button>
<button catchtap="noWork">明天不上班</button>  //阻止事件冒泡

六、数据双向绑定

1.设置值

vue中,只需要再表单元素上加上v-model,然后再绑定data中对应的一个值,当表单元素内容发生变化时,data中对应的值也会相应改变,这是vue非常nice的一点。

复制代码
<div id="app">
    <input v-model="reason" placeholder="填写理由" class='reason'/>
</div>

new Vue({
  el: '#app',
  data: {
   reason:''
  }
})
复制代码

但是在小程序中,却没有这个功能。那怎么办呢?
当表单内容发生变化时,会触发表单元素上绑定的方法,然后在该方法中,通过this.setData({key:value})来将表单上的值赋值给data中的对应值。
下面是代码,可以感受一下:

复制代码
<input bindinput="bindReason" placeholder="填写理由" class='reason' value='{{reason}}' name="reason" />

Page({
data:{
    reason:''
},
bindReason(e) {
    this.setData({
      reason: e.detail.value
    })
  }
})
复制代码

当页面表单元素很多的时候,更改值就是一件体力活了。和小程序一比较,vuev-model简直爽的不要不要的。

2.取值

vue中,通过this.reason取值

小程序中,通过this.data.reason取值

七、绑定事件传参

vue中,绑定事件传参挺简单,只需要在触发事件的方法中,把需要传递的数据作为形参传入就可以了,例如:

复制代码
<button @click="say('明天不上班')"></button>

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

 

小程序中,不能直接在绑定事件的方法中传入参数,需要将参数作为属性值,绑定到元素上的data-属性上,然后在方法中,通过e.currentTarget.dataset.*的方式获取,从而完成参数的传递,很麻烦有没有...

复制代码
<view class='tr' bindtap='toApprove' data-id="{{item.id}}"></view>
Page({
data:{
    reason:''
},
toApprove(e) {
    let id = e.currentTarget.dataset.id;
  }
})
复制代码

八、父子组件通信

1.子组件的使用

vue中,需要:

  1. 编写子组件
  2. 在需要使用的父组件中通过import引入
  3. vuecomponents中注册
  4. 在模板中使用
复制代码
//子组件 bar.vue
<template>
  <div class="search-box">
    <div @click="say" :title="title" class="icon-dismiss"></div>
  </div>
</template>
<script>
export default{
props:{
    title:{
       type:String,
       default:''
      }
    }
},
methods:{
    say(){
       console.log('明天不上班');
       this.$emit('helloWorld')
    }
}
</script>

// 父组件 foo.vue
<template>
  <div class="container">
    <bar :title="title" @helloWorld="helloWorld"></bar>
  </div>
</template>

<script>
import Bar from './bar.vue'
export default{
data:{
    title:"我是标题"
},
methods:{
    helloWorld(){
        console.log('我接收到子组件传递的事件了')
    }
},
components:{
    Bar
}
</script>
复制代码

 

小程序中,需要:

  1. 编写子组件
  2. 在子组件的json文件中,将该文件声明为组件

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

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

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

    具体代码:

    复制代码
    // 子组件
    <!--components/tabBar/tabBar.wxml-->
    <view class='tabbar-wrapper'>
      <view class='left-bar {{currentpage==="index"?"active":""}}' bindtap='jumpToIndex'>
        <text class='iconfont icon-shouye'></text>
        <view>首页</view>
      </view>
      <view class='right-bar {{currentpage==="setting"?"active":""}}' bindtap='jumpToSetting'>
        <text class='iconfont icon-shezhi'></text>
        <view>设置</view>
      </view>
    </view>
    复制代码

     

2.父子组件间通信

vue

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

复制代码
// 父组件 foo.vue
<template>
  <div class="container">
    <bar :title="title"></bar>
  </div>
</template>
<script>
import Bar from './bar.vue'
export default{
data:{
    title:"我是标题"
},
components:{
    Bar
}
</script>

// 子组件bar.vue
<template>
  <div class="search-box">
    <div :title="title" ></div>
  </div>
</template>
<script>
export default{
props:{
    title:{
       type:String,
       default:''
      }
    }
}
</script>
复制代码

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

小程序

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

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

此处, “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自定义名称事件,父组件中使用
    },
}

//父组件中
<bar bind:myevent="toggleToast"></bar>

// 获取子组件信息
toggleToast(e){
    console.log(e.detail)
}
复制代码

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

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

//子组件
<bar ref="bar"></bar>

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

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

//子组件
<bar id="bar"></bar>

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

 

 

 

 

 

 

.

Guess you like

Origin www.cnblogs.com/jianxian/p/11135467.html
Recommended