http://mpvue.com/mpvue/

美团开源了mpvue

由于mpvue框架是完全基于Vue框架的(重写了其runtime和compiler)

运行时框架 runtime 和代码编译器 compiler 实现

mp:mini program 的缩写

mpvue:Vue.js in mini program

  1. npm set registry https://registry.npm.taobao.org/
  1. npm install vue-cli -g

开始创建项目

  1. vue init mpvue/mpvue-quickstart mpvueTest

引入一些项目需要的基本第三方库

package.json,加入一下内容

  1. "dependencies": {
  2. "mpvue": "^2.0.0",
  3. "vuex": "^3.0.1",
  4. "weapp-qrcode": "^0.9.0",
  5. "flyio": "^0.5.9",
  6. "install": "^0.12.2",
  7. "mp-weui": "^1.0.3",
  8. "mpvue-zanui": "^1.0.2",
  9. "common-mpvue": "^0.4.6",
  10. "mpvue-config-loader": "^0.1.3"
  11. },

第三方库

  1. import Vue from 'vue'
  2. import App from './App'
  3. import store from '@/store';
  4. import WeUI from 'mp-weui/packages';
  5. Vue.use(WeUI);
  6. Vue.config.productionTip = false
  7. App.mpType = 'app'
  8. const app = new Vue({
  9. store,
  10. ...App
  11. });
  12. app.$mount();

main.json

  1. {
  2. "navigationBarTitleText": "页面标题",
  3. "enablePullDownRefresh": true
  4. }

utils目录下index.js常用方法:

  1. // /**
  2. // * 获取storage
  3. // */
  4. export function getCache(key) {
  5. var value = wx.getStorageSync(key)
  6. if (value) {
  7. return value
  8. }
  9. return ""
  10. }
  11. // /**
  12. // * 删除storage
  13. // */
  14. export function removeCache(key) {
  15. wx.removeStorage(key);
  16. }
  17. /**
  18. * 存储storage
  19. */
  20. export function setCache(key, value) {
  21. try {
  22. wx.setStorageSync(key, value)
  23. } catch (e) { }
  24. }
  25. /**
  26. 获取url参数
  27. */
  28. export function getUrlParam(path) {
  29. var result = {},
  30. param = /([^?=&]+)=([^&]+)/gi,
  31. match;
  32. while ((match = param.exec(path)) != null) {
  33. result[match[1]] = match[2];
  34. }
  35. return result;
  36. }
  37. /**
  38. 数组是否包含某个字符串
  39. */
  40. export const carrContainStr = (a, obj) => {
  41. for (var i = 0; i < a.length; i++) {
  42. if (a[i] == obj) {
  43. return i;
  44. }
  45. }
  46. return -1;
  47. }
  48. /**
  49. 克隆
  50. */
  51. export const clone = (obj) => {
  52. // Handle the 3 simple types, and null or undefined
  53. if (null == obj || "object" != typeof obj) return obj;
  54. // Handle Date
  55. if (obj instanceof Date) {
  56. var copy = new Date();
  57. copy.setTime(obj.getTime());
  58. return copy;
  59. }
  60. // Handle Array
  61. if (obj instanceof Array) {
  62. var copy = [];
  63. for (var i = 0, len = obj.length; i < len; ++i) {
  64. copy[i] = clone(obj[i]);
  65. }
  66. return copy;
  67. }
  68. // Handle Object
  69. if (obj instanceof Object) {
  70. var copy = {};
  71. for (var attr in obj) {
  72. if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
  73. }
  74. return copy;
  75. }
  76. throw new Error("Unable to copy obj! Its type isn't supported.");
  77. }
  78. /**
  79. 判断机型
  80. */
  81. export const isiOS = () => {
  82. var u = navigator.userAgent;
  83. var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
  84. var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  85. if (isiOS) {
  86. return true
  87. }
  88. return false;
  89. }

https://vuex.vuejs.org/zh/guide/

flyio封装网络请求



flyio请求,封装代码

  1. var Fly=require("flyio/dist/npm/wx")
  2. import { getCache } from '../utils'
  3. const request = new Fly()
  4. // 全局加载提示 - 设定时间
  5. let ltime = 0;
  6. function closeLoading(param) {
  7. ltime--
  8. }
  9. request.interceptors.request.use((request) => {
  10. // 全局加载提示 - 展示提示
  11. // wx.showNavigationBarLoading()
  12. ltime++
  13. let dataSource = getCache("dataSource")
  14. request.headers = {
  15. "Content-Type": "application/x-www-form-urlencoded",
  16. "source": "miniApp",
  17. "dataSource": dataSource ? dataSource : ''
  18. }
  19. // 没用到
  20. if (request.url.indexOf('getReviewInfo') != -1) {
  21. closeLoading()
  22. return request
  23. }
  24. // 登录
  25. console.log('这是token');
  26. console.log();
  27. let type = '';
  28. if(request.url.indexOf("wxLogin") != -1) {
  29. type = request.body.loginType;
  30. }
  31. console.log(getCache("token"));
  32. console.log('这是token');
  33. if (request.url.indexOf("wxLogin") == -1 || type == 'WORKBENCH') {
  34. // let storeId = getCache("storeId");
  35. let storeCode = getCache("storeCode");
  36. let inviter = getCache("inviter");
  37. let token = getCache("token");
  38. request.headers = {
  39. "Content-Type": "application/x-www-form-urlencoded",
  40. "source": "miniApp",
  41. "token": token,
  42. "storeCode": storeCode,
  43. "inviter": inviter
  44. }
  45. console.log('打印request');
  46. console.log(request);
  47. console.log('打印request');
  48. let dataSource = getCache("dataSource")
  49. if (dataSource) {
  50. request.headers['dataSource'] = dataSource
  51. }
  52. }
  53. return request
  54. })
  55. request.interceptors.response.use((response, promise) => {
  56. closeLoading()
  57. // wx.hideNavigationBarLoading()
  58. // 微信运维统计
  59. if (response.status) {
  60. wx.reportMonitor('0', +(response.status))
  61. }
  62. if (response.headers.date) {
  63. let time = new Date().getTime() - new Date(response.headers.date).getTime()
  64. wx.reportMonitor('1', +(time))
  65. }
  66. // 错误提示
  67. if (response.status != 200) {
  68. wx.showToast({
  69. title: '出错啦!请稍后再试试哦~',
  70. icon: 'none',
  71. duration: 2000
  72. })
  73. }
  74. return promise.resolve(response.data)
  75. },
  76. (err, promise) => {
  77. wx.hideNavigationBarLoading()
  78. return promise.resolve()
  79. }
  80. )
  81. export default request

新建utils/api.js文件

  1. export const baseUrlApi = 'http://:8080'//---开发调试环境
  2. //export const baseUrlApi = 'https://test.mini.com'//---测试环境https
  3. //export const baseUrlApi = 'https://product.mini.com'//---生产环境https

src下新建service文件夹

login-service.js,order-service.js

  1. import { baseUrlApi } from '../utils/api'
  2. import request from '../utils/request'
  3. export default {
  4. // 登录
  5. wxLogin: (data) =>
  6. request.post(`/store-miniApp-web/external/interface/wechat/wxLogin`, data, { baseURL: baseUrlApi }),
  7. // 收藏
  8. addCollect: (goodId, status) =>
  9. request.get(`/store-miniApp-web/store/member/addCollect?goodId=${goodId}&status=${status}`,
  10. null, {
  11. baseURL: baseUrlApi
  12. }),
  13. }

接口请求的使用

  1. import loginApi from "@/service/login-service";
  2. methods: {
  3. //-登录
  4. clickLoginBtn() {
  5. var data = {
  6. phone: '',
  7. password: "",
  8. };
  9. console.log("登录参数==", data);
  10. loginApi.wxLogin(data).then(
  11. data => {
  12. if (!data) {
  13. this.$toast(data.msg);
  14. return;
  15. }
  16. if (data.code==0) {
  17. console.log("登录成功", data);
  18. }
  19. },
  20. err => {
  21. }
  22. );
  23. },
  24. //-收藏
  25. collect() {
  26. let isCollect = "1"; //1收藏 0取消
  27. let goodId = "";
  28. loginApi.addCollect(goodsId, isCollect).then(data => {
  29. if (data.code != 0) {
  30. console.log("收藏失败", data);
  31. return;
  32. }
  33. if (isCollect == 1) {
  34. this.$toast("取消成功");
  35. } else {
  36. this.$toast("收藏成功");
  37. }
  38. });
  39. }
  40. }

mapGetters

mapMutations

  1. const store new Vuex.Store({
  2. state: {
  3. count: 0
  4. },
  5. mutations: {
  6. increment(state,n){
  7. state.count += n
  8. }
  9. }
  10. })
  11. new Vue({
  12. el:"#app",
  13. store,
  14. computed: {
  15. count() {
  16. return store.state.count
  17. }
  18. },
  19. methods: {
  20. add() {
  21. //传参
  22. store.commit('increment',10)
  23. }
  24. }
  25. })

mapMutations辅助函数的传参

  1. //store.js
  2. import Vue from 'vue'
  3. import Vuex from 'vuex'
  4. Vue.use(Vuex)
  5. //定义state,并将listName设置为一个空对象
  6. const state = {
  7. listName: {}
  8. }
  9. //定义mutations,可以传参,用于设置state里的listName
  10. const mutations = {
  11. set_listname: (state,value) => {
  12. state.listName=value
  13. }
  14. }
  15. //定义getters,用于获取出state里的对象
  16. const getters = {
  17. get_listname: state => {
  18. return state.listName
  19. }
  20. }
  21. export default new Vuex.Store({
  22. getters,
  23. state,
  24. mutations
  25. })

在vue实例中注册store

  1. //main.js
  2. import Vue from 'vue'
  3. import App from './App'
  4. import store from './store'
  5. /* eslint-disable no-new */
  6. new Vue({
  7. el: '#app',
  8. router,
  9. store,
  10. template: '<App/>',
  11. components: { App }
  12. })



Fly.js

一个支持所有JavaScript运行环境的基于Promise的、支持请求转发、强大的http请求库。可以让您在多个端上尽可能大限度的实现代码复用。

https://wendux.github.io/dist/#/doc/flyio/readme

vuex的定义

Vuex 是一个专门为 Vue.js 应用程序开发的状态管理模式

集中存储和管理应用的所有组件的状态

store(仓库),new Vue.store({…})


请点赞!因为你的鼓励是我写作的最大动力!

吹逼交流群:711613774

mpvue搭建小程序框架的更多相关文章

  1. 基于mpvue搭建小程序项目框架

    简介: mpvue框架对于从没有接触过小程序又要尝试小程序开发的人员来说,无疑是目前最好的选择.mpvue从底层支持 Vue.js 语法和构建工具体系,同时再结合相关UI组件库,便可以高效的实现小程序 ...

  2. 美团小程序框架mpvue入门

    mpvue 主要特性 使用 mpvue 开发小程序,你将在小程序技术体系的基础上获取到这样一些能力: 1. 彻底的组件化开发能力:提高代码复用性 2. 完整的 Vue.js 开发体验 3. 方便的 V ...

  3. 美团小程序框架mpvue蹲坑指南

    美团小程序框架mpvue(花名:没朋友)蹲坑指南 第一次接触小程序大概是17年初,当时小程序刚刚内侧,当时就被各种限制折腾的死去活来的,单向绑定, 没有promise,请求数限制,包大小限制,各种反人 ...

  4. mpvue最佳实践 , 美团出的一个小程序框架

    看手机微信,看到说美团出了1个小程序框架,  mpvue 搜下来试试,看了网上的一个对比 ----------------- 以下为引用 我们对微信小程序.mpvue.WePY 这三个开发框架的主要能 ...

  5. 小程序框架MpVue踩坑日记(一)

    小程序也做了几个小功能模块了,总觉得需要总结一下,踩坑什么的还是得记录一下啊. 好吧,其实是为了方便回顾 首先,说到小程序框架,大家都知道wepy,不过,我是没用过 美团开发团队到mpvue到是个实在 ...

  6. 美团小程序框架mpvue入门教程

    mpvue是一个使用 Vue.js 开发小程序的前端框架.框架基于 Vue.js 核心,mpvue 修改了 Vue.js 的 runtime 和 compiler 实现,使其可以运行在小程序环境中,从 ...

  7. 使用mpvue开发小程序教程(二)

    在上篇文章中,我们介绍了使用mpvue开发小程序所需要的一些开发环境的搭建,并创建了第一个mpvue小程序代码骨架并将其运行起来.在本文中,我们来研究熟悉一下mpvue项目的主要目录和文件结构. 在V ...

  8. 为什么选择MpVue进行小程序的开发

    前言 mpvue是一款使用Vue.js开发微信小程序的前端框架.使用此框架,开发者将得到完整的 Vue.js 开发体验,同时为H5和小程序提供了代码复用的能力.如果想将 H5 项目改造为小程序,或开发 ...

  9. 学习笔记:mpvue开发小程序——入门

    接下来可能要开发一个小程序,同事推荐使用mpvue,那么我提前熟悉下. 官网地址:http://mpvue.com/ 1.快速上手 http://mpvue.com/mpvue/quickstart/ ...

随机推荐

  1. 记录:拷贝gitblit里的项目使用git命令clone、pull、push等,出现一直在加载,卡住没反应的问题

    俺想克隆别人gitblit里的其中一个版本库(俺在别人gitblit有权限) 懂得git的道友们,都应该知道克隆一个公共项目,随便找到,打开git终端,输入git clone 项目地址就行了 到了俺这 ...

  2. C# 文件类中 File ,FileInfo 类的主要区别

    System.IO命名空间中提供的文件操作类有File和FileInfo,这两个类的功能基本相同,只是File是静态类,其中所有方法都是静态的,可以通过类名直接调用,不需要实例化.而FileInfo是 ...

  3. IIS配置文件的XML格式不正确 applicationHost.config崩溃

    错误提示如图: 检查C:\Windows\System32\inetsrv\config目录下的applicationHost.config文件,备份一份. 可使用IIS提供的AppCmd.exe的r ...

  4. nlp-roadmap

    nlp-roadmap https://github.com/graykode/nlp-roadmap nlp-roadmap is Natural Language Processing ROADM ...

  5. 【kubernetes】通过rancher2部署k8s

    1. K8S相关介绍 十分钟带你理解Kubernetes核心概念 2. 部署rancher # 更新操作系统软件包 yum update -y # 删除历史容器及数据 docker rm -f $(d ...

  6. cal of easter egg

    在Linux中用cal命令查看1752年9月,发现3到13日全都消失了……

  7. html相对字体

    文章:使用 rem 设置文字大小 使用rem作为字体单位.

  8. Fiddler使用资料-整理

    以下是一个博主写的一个系列. 随笔分类 - Fiddler   10.Fiddler中设置断点修改Response 摘要:当然Fiddler中也能修改Response 第一种:打开Fiddler 点击 ...

  9. Vue中mapMutations映射方法的问题

    今天又被自己给蠢到,找了半天没发现问题.大家看下代码. mutation-types.js 里我新增了一个类型.INIT_CURRENTORDER export const GET_USERINFO ...

  10. lomback插件在日志管理方面的应用

    由于现在使用日志可以省去在解决bug时候的很多麻烦, lomback为我们提供了很方便的打印日志的管理 @RunWith(SpringRunner.class) @SpringBootTest @Sl ...