vue 和微信小程序的区别、比较

本文详细比较了Vue和微信小程序在数据绑定、列表渲染、显示/隐藏、事件处理、双向绑定以及父子组件通信方面的异同,展示了两者在开发中的应用和差异。
摘要由CSDN通过智能技术生成

数据绑定:

VUE:vue动态绑定一个变量的值为元素的某个属性的时候,会在变量前面加上冒号:

<img :src="imgSrc"/>

小程序:绑定某个变量的值为元素属性时,会用两个大括号括起来,如果不加括号,为被认为是字符串。例:

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

列表渲染

vue:

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

小程序

<text wx:for="{{items}}">{{item}}</text>

显示与隐藏

vue中,使用v-if 和v-show控制元素的显示和隐藏
小程序中,使用wx-if和hidden控制元素的显示和隐藏

事件处理

vue:习惯@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>  //阻止事件冒泡

双向绑定

在vue中,只需要再表单元素上加上v-model
小程序 没有

取data 里面的值

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;
  }
})

父子组件通信

vue

//子组件 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(){
    return{
        title:"我是标题"
    }
},
methods:{
    helloWorld(){
        console.log('我接收到子组件传递的事件了')
    }
},
components:{
    Bar
}
</script>

父组件向子组件传递数据,只需要在父组件通过v-bind传入一个值,在子组件中,通过props接收,即可完成数据的传递
子组件和父组件通信可以通过this.$emit将方法和数据传递给父组件。
小程序
在子组件的json文件中,将该文件声明为组件

{
  "component": true
}

在需要引入的父组件的json文件中,在usingComponents填写引入组件的组件名以及路径

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

在父组件中,直接引入即可

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

父组件向子组件通信 直接将值赋值给一个变量

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

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

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

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

子组件向父组件通信 代码如下:

//子组件中
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)
}

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值