1、部分微信小程序默认属性值需要通过变量传递

如:

  1. <!-- 部分属性在原始小程序中使用 -->
  2. <image show-menu-by-longpress=true />
  3. <!-- 部分属性在mpvue中使用 -->
  4. <img :show-menu-by-longpress="true" />
  5. <image :show-menu-by-longpress="true" />

2、页面不支持过滤器

所以页面需要进行格式化处理的,必须先对要展示的数据进行预处理。

3、不建议使用VUE的生命周期,因为VUE的生命周期先于微信小程序的生命周期。

关于常用语法问题 - 图1

4、Class 与 Style 绑定

class 支持的语法:

  1. <p :class="{ active: isActive }">111</p>
  2. <p class="static" v-bind:class="{ active: isActive, 'text-danger': hasError }">222</p>
  3. <p class="static" :class="[activeClass, errorClass]">333</p>
  4. <p class="static" v-bind:class="[isActive ? activeClass : '', errorClass]">444</p>
  5. <p class="static" v-bind:class="[{ active: isActive }, errorClass]">555</p>

将分别被转换成:

  1. <view class="_p {{[isActive ? 'active' : '']}}">111</view>
  2. <view class="_p static {{[isActive ? 'active' : '', hasError ? 'text-danger' : '']}}">222</view>
  3. <view class="_p static {{[activeClass, errorClass]}}">333</view>
  4. <view class="_p static {{[isActive ? activeClass : '', errorClass]}}">444</view>
  5. <view class="_p static {{[[isActive ? 'active' : ''], errorClass]}}">555</view>

style 支持的语法:

  1. <p v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">666</p>
  2. <p v-bind:style="[{ color: activeColor, fontSize: fontSize + 'px' }]">777</p>

将分别被转换成:

  1. <view class="_p" style=" {{'color:' + activeColor + ';' + 'font-size:' + fontSize + 'px' + ';'}}">666</view>
  2. <view class="_p" style=" {{'color:' + activeColor + ';' + 'font-size:' + fontSize + 'px' + ';'}}">777</view>

如果子组件中需要使用 class 或者 style 则需要通过 props 变量传递,而 class 需要写在全局中。

5、列表渲染

注意:只是需要注意一点,嵌套列表渲染,必须指定不同的索引!

  1. <!-- 在这种嵌套循环的时候, index 和 itemIndex 这种索引是必须指定,且别名不能相同,正确的写法如下 -->
  2. <template>
  3. <ul v-for="(card, index) in list">
  4. <li v-for="(item, itemIndex) in card">
  5. {{item.value}}
  6. </li>
  7. </ul>
  8. </template>

6、事件映射表

注意:小程序中事件绑定默认使用bind开头,后面才是事件

  1. // 事件映射表,左侧为 WEB 事件,右侧为 小程序 对应事件
  2. {
  3. click: 'tap',
  4. touchstart: 'touchstart',
  5. touchmove: 'touchmove',
  6. touchcancel: 'touchcancel',
  7. touchend: 'touchend',
  8. tap: 'tap',
  9. longtap: 'longtap',
  10. input: 'input',
  11. change: 'change',
  12. submit: 'submit',
  13. blur: 'blur',
  14. focus: 'focus',
  15. reset: 'reset',
  16. confirm: 'confirm',
  17. columnchange: 'columnchange',
  18. linechange: 'linechange',
  19. error: 'error',
  20. scrolltoupper: 'scrolltoupper',
  21. scrolltolower: 'scrolltolower',
  22. scroll: 'scroll'
  23. }


7、v-html指令使用

v-html 指令会将节点转化为 rich-text 节点,默认使用节点的 string 模式。如果需要修改样式,则需要使用正则匹配,替换其中的属性值。
如:

  1. <div v-html="contentHtml"></div>
  2. 将被转化为
  3. <rich-text :nodes="contentHtml"></rich-text>

8、使用VUEX

找到你需要使用VUEX的页面的文件夹,找到其中的main.js。
打开添加

  1. import store from '@/store';
  2. Vue.prototype.$store = store;

建议直接在整个APP上添加,那么所有的页面都能共享一颗状态树,好处是前一个页面获取了用户信息存在状态树上后,后面的页面可以通过状态树进行获取,相当于缓存的作用。

9、使用fly.js替代默认请求工具

这个工具类似于axios,也被promise化了。

使用方式
新建一个fly.js

  1. import { timeout } from '@/config/env';
  2. let Fly = require('flyio/dist/npm/wx');
  3. let fly = new Fly();
  4. // 添加请求拦截器
  5. fly.interceptors.request.use(
  6. (request, promise) => {
  7. // console.log('promise: ', promise);
  8. // 给所有请求添加自定义header
  9. request.headers['Content-Type'] = 'application/json;charset=UTF-8';
  10. // 打印出请求体
  11. console.log(request.body);
  12. // 终止请求
  13. // var err=new Error("xxx")
  14. // err.request=request
  15. // return Promise.reject(new Error(""))
  16. // 可以显式返回request, 也可以不返回,没有返回值时拦截器中默认返回request
  17. return request;
  18. },
  19. (err, promise) => {
  20. wx.showModal({
  21. title: '网络错误',
  22. content: err.message,
  23. showCancel: false,
  24. success(res) {
  25. if (res.confirm) {
  26. console.log('用户点击确定');
  27. } else if (res.cancel) {
  28. console.log('用户点击取消');
  29. }
  30. }
  31. });
  32. return promise.reject(err);
  33. }
  34. );
  35. // 添加响应拦截器,响应拦截器会在then/catch处理之前执行
  36. fly.interceptors.response.use(
  37. (response, promise) => {
  38. // console.log('promise: ', promise);
  39. // 只将请求结果的data字段返回
  40. if (response.data && response.data.error_no && response.data.error_no === '0') {
  41. return response.data;
  42. } else {
  43. wx.showModal({
  44. title: '网络请求错误',
  45. content: `${response.data.error_no}:${response.data.error_info}`,
  46. showCancel: false,
  47. success(res) {
  48. if (res.confirm) {
  49. console.log('用户点击确定');
  50. } else if (res.cancel) {
  51. console.log('用户点击取消');
  52. }
  53. }
  54. });
  55. return {};
  56. }
  57. },
  58. (err, promise) => {
  59. // 发生网络错误后会走到这里
  60. wx.showModal({
  61. title: '网络错误',
  62. content: err.message,
  63. showCancel: false,
  64. success(res) {
  65. if (res.confirm) {
  66. console.log('用户点击确定');
  67. } else if (res.cancel) {
  68. console.log('用户点击取消');
  69. }
  70. }
  71. });
  72. return promise.reject(err);
  73. }
  74. );
  75. fly.config.timeout = timeout;
  76. export default fly;

找到APP的主main.js文件。

  1. import fly from '@/data/fly';
  2. Vue.prototype.$http = fly;
  3. global.$http = fly;

页面中使用

  1. this.$http.get('http://www.baidu.com')