1、部分微信小程序默认属性值需要通过变量传递
如:
<!-- 部分属性在原始小程序中使用 --><image show-menu-by-longpress=true /><!-- 部分属性在mpvue中使用 --><img :show-menu-by-longpress="true" /><image :show-menu-by-longpress="true" />
2、页面不支持过滤器
所以页面需要进行格式化处理的,必须先对要展示的数据进行预处理。
3、不建议使用VUE的生命周期,因为VUE的生命周期先于微信小程序的生命周期。

4、Class 与 Style 绑定
class 支持的语法:
<p :class="{ active: isActive }">111</p><p class="static" v-bind:class="{ active: isActive, 'text-danger': hasError }">222</p><p class="static" :class="[activeClass, errorClass]">333</p><p class="static" v-bind:class="[isActive ? activeClass : '', errorClass]">444</p><p class="static" v-bind:class="[{ active: isActive }, errorClass]">555</p>
将分别被转换成:
<view class="_p {{[isActive ? 'active' : '']}}">111</view><view class="_p static {{[isActive ? 'active' : '', hasError ? 'text-danger' : '']}}">222</view><view class="_p static {{[activeClass, errorClass]}}">333</view><view class="_p static {{[isActive ? activeClass : '', errorClass]}}">444</view><view class="_p static {{[[isActive ? 'active' : ''], errorClass]}}">555</view>
style 支持的语法:
<p v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">666</p><p v-bind:style="[{ color: activeColor, fontSize: fontSize + 'px' }]">777</p>
将分别被转换成:
<view class="_p" style=" {{'color:' + activeColor + ';' + 'font-size:' + fontSize + 'px' + ';'}}">666</view><view class="_p" style=" {{'color:' + activeColor + ';' + 'font-size:' + fontSize + 'px' + ';'}}">777</view>
如果子组件中需要使用 class 或者 style 则需要通过 props 变量传递,而 class 需要写在全局中。
5、列表渲染
注意:只是需要注意一点,嵌套列表渲染,必须指定不同的索引!
<!-- 在这种嵌套循环的时候, index 和 itemIndex 这种索引是必须指定,且别名不能相同,正确的写法如下 --><template><ul v-for="(card, index) in list"><li v-for="(item, itemIndex) in card">{{item.value}}</li></ul></template>
6、事件映射表
注意:小程序中事件绑定默认使用bind开头,后面才是事件
// 事件映射表,左侧为 WEB 事件,右侧为 小程序 对应事件{click: 'tap',touchstart: 'touchstart',touchmove: 'touchmove',touchcancel: 'touchcancel',touchend: 'touchend',tap: 'tap',longtap: 'longtap',input: 'input',change: 'change',submit: 'submit',blur: 'blur',focus: 'focus',reset: 'reset',confirm: 'confirm',columnchange: 'columnchange',linechange: 'linechange',error: 'error',scrolltoupper: 'scrolltoupper',scrolltolower: 'scrolltolower',scroll: 'scroll'}
7、v-html指令使用
v-html 指令会将节点转化为 rich-text 节点,默认使用节点的 string 模式。如果需要修改样式,则需要使用正则匹配,替换其中的属性值。
如:
<div v-html="contentHtml"></div>将被转化为<rich-text :nodes="contentHtml"></rich-text>
8、使用VUEX
找到你需要使用VUEX的页面的文件夹,找到其中的main.js。
打开添加
import store from '@/store';Vue.prototype.$store = store;
建议直接在整个APP上添加,那么所有的页面都能共享一颗状态树,好处是前一个页面获取了用户信息存在状态树上后,后面的页面可以通过状态树进行获取,相当于缓存的作用。
9、使用fly.js替代默认请求工具
这个工具类似于axios,也被promise化了。
使用方式
新建一个fly.js
import { timeout } from '@/config/env';let Fly = require('flyio/dist/npm/wx');let fly = new Fly();// 添加请求拦截器fly.interceptors.request.use((request, promise) => {// console.log('promise: ', promise);// 给所有请求添加自定义headerrequest.headers['Content-Type'] = 'application/json;charset=UTF-8';// 打印出请求体console.log(request.body);// 终止请求// var err=new Error("xxx")// err.request=request// return Promise.reject(new Error(""))// 可以显式返回request, 也可以不返回,没有返回值时拦截器中默认返回requestreturn request;},(err, promise) => {wx.showModal({title: '网络错误',content: err.message,showCancel: false,success(res) {if (res.confirm) {console.log('用户点击确定');} else if (res.cancel) {console.log('用户点击取消');}}});return promise.reject(err);});// 添加响应拦截器,响应拦截器会在then/catch处理之前执行fly.interceptors.response.use((response, promise) => {// console.log('promise: ', promise);// 只将请求结果的data字段返回if (response.data && response.data.error_no && response.data.error_no === '0') {return response.data;} else {wx.showModal({title: '网络请求错误',content: `${response.data.error_no}:${response.data.error_info}`,showCancel: false,success(res) {if (res.confirm) {console.log('用户点击确定');} else if (res.cancel) {console.log('用户点击取消');}}});return {};}},(err, promise) => {// 发生网络错误后会走到这里wx.showModal({title: '网络错误',content: err.message,showCancel: false,success(res) {if (res.confirm) {console.log('用户点击确定');} else if (res.cancel) {console.log('用户点击取消');}}});return promise.reject(err);});fly.config.timeout = timeout;export default fly;
找到APP的主main.js文件。
import fly from '@/data/fly';Vue.prototype.$http = fly;global.$http = fly;
页面中使用
this.$http.get('http://www.baidu.com')
