美文网首页
vue和微信语法区别

vue和微信语法区别

作者: 小柠有点萌 | 来源:发表于2025-04-08 14:48 被阅读0次

1.事件定义区别

【1】vue事件绑定:

<button @click="dl">登陆</button>
1
<input type="text" @input="input"/>
1

vue用@来绑定事件

【2】微信小程序绑定:

<button bindtap="dl">登陆</button>
1
<input type="text" bindinput="input"/>
1

微信小程序原声写法前面加bind后面加事件名

2.事件函数传值

【1】vue事件函数传值:

<button @click="dl(index)">登陆</button>
1

vue传值直接写在函数括号中

【2】微信小程序事件函数传值:

<button bindtap="dl" data-index="{{index}}">登陆</button>
1

微信小程序传值需要用data-自定义名字={{需要传递的值}}

3.if语句

【1】vue的if语句:

<div v-if="index == 1">1<div>
<div v-else-if="index == 2">2<div>
<div v-else>3<div>
1
2
3

if语句前加 “v-”

【2】微信小程序if语句:

<view wx:if="{{index == 1}}">1<view>
<view wx:elif="{{index == 2}}">2<view>
<view wx:else>3<view>
1
2
3

微信小程序if语句前加 “wx:”,而且判断内容需要用 {{双大括号}} 包起来,第二行的else-if,直接省略成elif

4.关键字引用

【1】vue的关键字引用:

<img :src="src" />
1

vue中属性名前面加 “:” 或者 v-bind

【2】微信小程序关键字引用:

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

微信小程序中只需要属性值加“{{}}”包起来就行

5.for列表渲染

【1】vue的for列表渲染:

<block v-for="(item,i) in list" :key="i">
    <div>{{item.text}}</div>
</block >
1
2
3

vue的for语法=(每一项数据名,索引名) in 数据名

【2】微信小程序的for列表渲染:

<block wx:for="{{list}}" wx:data-item="item" wx:data-index="idx" wx:key="idx">
    <div>{{item.text}}</div>
</block >
1
2
3

微信小程序的for里只写数据名, 如果需要修改当前元素的数据名:wx:data-item=“自定义的数据名” 如果需要修改当前元素的索引名:wx:data-index=“自定义的索引名” 默认数据名是item,默认索引名是index

6、显示与隐藏元素

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

7、事件处理

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

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

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

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

8、数据双向绑定

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

1.  <div id="app">  
2.  <input v-model="reason" placeholder="填写理由" class='reason'/>  
3.  </div>  

5.  new Vue({  
6.  el: '#app',  
7.  data: {  
8.  reason:''  
9.  }  
10.  })  

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

1.  <input bindinput="bindReason" placeholder="填写理由" 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.  }) 

9.取值

vue中,通过this.reason取值

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

10.绑定事件传参

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

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

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

2.  Page({  
3.  data:{  
4.  reason:''  
5.  },  
6.  toApprove(e) {  
7.  let id = e.currentTarget.dataset.id;  
8.  }  
9.  })  

11.父子组件通信

1.子组件的使用
在vue中,需要:

编写子组件
在需要使用的父组件中通过import引入
在vue的components中注册
在模板中使用

1.  //子组件 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.  }, 

17.  methods:{  
18.  say(){ 
19.  console.log('明天不上班');  
20.  this.$emit('helloWorld')  
21.  }  
22.  } 
23.  </script>  

25.  // 父组件 foo.vue  
26.  <template>  
27.  <div class="container">  
28.  <bar :title="title" @helloWorld="helloWorld"></bar>  
29.  </div>  
30.  </template>  

32.  <script>  
33.  import Bar from './bar.vue'  
34.  export default{  
35.  data:{  
36.  title:"我是标题"  
37.  },  
38.  methods:{  
39.  helloWorld(){  
40.  console.log('我接收到子组件传递的事件了')  
41.  }  
42.  },  
43.  components:{ 
44.  Bar  
45.  }  
46.  </script>  

在小程序中,需要:
1.编写子组件

  1. 在子组件的json文件中,将该文件声明为组件
2.  "component": true  
3.  }  

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

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

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

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

具体代码:

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>首页</view>  
7.  </view>  
8.  <view class='right-bar {{currentpage==="setting"?"active":""}}' bindtap='jumpToSetting'>  
9.  <text class='iconfont icon-shezhi'></text>  
10.  <view>设置</view>  
11.  </view>  
12.  </view>  

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

1.  // 父组件 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:"我是标题"  
12.  },  
13.  components:{  
14.  Bar  
15.  }  
16.  </script>  

18.  // 子组件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>  

子组件和父组件通信可以通过this.$emit将方法和数据传递给父组件。
在小程序中
父组件向子组件通信和vue类似,但是小程序没有通过v-bind,而是直接将值赋值给一个变量,如下:

此处, “index”就是要向子组件传递的值
在子组件properties中,接收传递的值

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

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

1.  //子组件中  
2.  methods: {     
3.  // 传递给父组件  
4.  cancelBut: function (e) {  
5.  var that = this;  
6.  var myEventDetail = { pickerShow: false, type: 'cancel' } // detail对象,提供给事件监听函数  
7.  this.triggerEvent('myevent', myEventDetail) //myevent自定义名称事件,父组件中使用  
8.  }, 
9.  }  
10.  //父组件中  
11.  <bar bind:myevent="toggleToast"></bar>   
12.  // 获取子组件信息  
13.  toggleToast(e){  
14.  console.log(e.detail)  
15.  }  

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

1.  //子组件  
2.  <bar ref="bar"></bar>  
3.  //父组件  
4.  this.$ref.bar.子组件的方法  

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

1.  //子组件  
2.  <bar id="bar"></bar>  
3.  // 父组件  
4.  this.selectComponent('#id').syaHello()  

小程序和vue在这点上太相似了,有木有。。


image.png

相关文章

  • mpvue

    问:mpvue里,哪些是要用微信小程序原生写法?(也就是mpvue和vue的区别) 答:vue里跳转页面,用

  • 3月总结

    Vue2.x 和vue3.x的语法区别 Router.js创建路由 Vue3.x需要引入createRouter创...

  • wepy体验 之 环境搭建

    wepy是由微信官方研发的一个类vue小程序框架,语法基本与vue相同。它有什么优点,大家可以到它的官网看微信是怎...

  • uniapp

    开发工具开发者中心如何学习?vue和微信小程序的区别、比较uni-app官方教程学习手记Vue单文件组件 (SFC...

  • VUE的学习笔记

    vue和react的区别。 vue1.0是双向绑定的,vue2.0是单向绑定的,v-model是一个语法糖。rea...

  • 微信小程序和Vue的区别

    单向绑定 & 双向绑定 微信小程序只支持单向绑定,如果需要修改data的值,只能通过setData操作。Vue可以...

  • 微信小程序和vue的区别

    数据类型 vue组件中data必须是函数 data(){return{}},new Vue中的选项可以是函数也可以...

  • 微信小程序数据绑定

    微信小程序的数据绑定和Vue很相似,都是用mustache(大胡子)语法,数据定义在data中。 可以在里边做一些...

  • mpvue是什么?如何快速的创建一个mpvue项目,并且运行,写

    Mpvue是一个基于Vue的微信小程序前端框架,可以让我们用vue的语法写小程序的项目。 1,检查node是否安装...

  • 网络营销干货资讯分享:要想微信微博运营好,二者区别要明了!

    今天我们聊一聊微信和微博的区别,大部分人都知道,微信是微信,微博是微博,肯定是有区别的,但有哪些区别并不太清楚,觉...

网友评论

      本文标题:vue和微信语法区别

      本文链接:https://www.haomeiwen.com/subject/dvcifdtx.html