mpvue搭建小程序框架
美团开源了mpvue
由于mpvue框架是完全基于Vue框架的(重写了其runtime和compiler)
运行时框架 runtime 和代码编译器 compiler 实现
mp:mini program 的缩写
mpvue:Vue.js in mini program
mpvue-loader 提供 webpack 版本的加载器
mpvue-webpack-target webpack 构建目标
postcss-mpvue-wxss 样式代码转换预处理工具
px2rpx-loader 样式转化插件
mpvue-quickstart mpvue-quickstart
mpvue-simple 辅助 mpvue 快速开发 Page / Component 级小程序页面的工具
npm set registry https://registry.npm.taobao.org/
npm install vue-cli -g
开始创建项目
vue init mpvue/mpvue-quickstart mpvueTest
引入一些项目需要的基本第三方库
package.json,加入一下内容
"dependencies": {
"mpvue": "^2.0.0",
"vuex": "^3.0.1",
"weapp-qrcode": "^0.9.0",
"flyio": "^0.5.9",
"install": "^0.12.2",
"mp-weui": "^1.0.3",
"mpvue-zanui": "^1.0.2",
"common-mpvue": "^0.4.6",
"mpvue-config-loader": "^0.1.3"
},
第三方库
import Vue from 'vue'
import App from './App'
import store from '@/store';
import WeUI from 'mp-weui/packages';
Vue.use(WeUI);
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
store,
...App
});
app.$mount();
main.json
{
"navigationBarTitleText": "页面标题",
"enablePullDownRefresh": true
}
utils目录下index.js常用方法:
// /**
// * 获取storage
// */
export function getCache(key) {
var value = wx.getStorageSync(key)
if (value) {
return value
}
return ""
}
// /**
// * 删除storage
// */
export function removeCache(key) {
wx.removeStorage(key);
}
/**
* 存储storage
*/
export function setCache(key, value) {
try {
wx.setStorageSync(key, value)
} catch (e) { }
}
/**
获取url参数
*/
export function getUrlParam(path) {
var result = {},
param = /([^?=&]+)=([^&]+)/gi,
match;
while ((match = param.exec(path)) != null) {
result[match[1]] = match[2];
}
return result;
}
/**
数组是否包含某个字符串
*/
export const carrContainStr = (a, obj) => {
for (var i = 0; i < a.length; i++) {
if (a[i] == obj) {
return i;
}
}
return -1;
}
/**
克隆
*/
export const clone = (obj) => {
// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;
// Handle Date
if (obj instanceof Date) {
var copy = new Date();
copy.setTime(obj.getTime());
return copy;
}
// Handle Array
if (obj instanceof Array) {
var copy = [];
for (var i = 0, len = obj.length; i < len; ++i) {
copy[i] = clone(obj[i]);
}
return copy;
}
// Handle Object
if (obj instanceof Object) {
var copy = {};
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
}
/**
判断机型
*/
export const isiOS = () => {
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
if (isiOS) {
return true
}
return false;
}
https://vuex.vuejs.org/zh/guide/
flyio封装网络请求
flyio请求,封装代码
var Fly=require("flyio/dist/npm/wx")
import { getCache } from '../utils'
const request = new Fly()
// 全局加载提示 - 设定时间
let ltime = 0;
function closeLoading(param) {
ltime--
}
request.interceptors.request.use((request) => {
// 全局加载提示 - 展示提示
// wx.showNavigationBarLoading()
ltime++
let dataSource = getCache("dataSource")
request.headers = {
"Content-Type": "application/x-www-form-urlencoded",
"source": "miniApp",
"dataSource": dataSource ? dataSource : ''
}
// 没用到
if (request.url.indexOf('getReviewInfo') != -1) {
closeLoading()
return request
}
// 登录
console.log('这是token');
console.log();
let type = '';
if(request.url.indexOf("wxLogin") != -1) {
type = request.body.loginType;
}
console.log(getCache("token"));
console.log('这是token');
if (request.url.indexOf("wxLogin") == -1 || type == 'WORKBENCH') {
// let storeId = getCache("storeId");
let storeCode = getCache("storeCode");
let inviter = getCache("inviter");
let token = getCache("token");
request.headers = {
"Content-Type": "application/x-www-form-urlencoded",
"source": "miniApp",
"token": token,
"storeCode": storeCode,
"inviter": inviter
}
console.log('打印request');
console.log(request);
console.log('打印request');
let dataSource = getCache("dataSource")
if (dataSource) {
request.headers['dataSource'] = dataSource
}
}
return request
})
request.interceptors.response.use((response, promise) => {
closeLoading()
// wx.hideNavigationBarLoading()
// 微信运维统计
if (response.status) {
wx.reportMonitor('0', +(response.status))
}
if (response.headers.date) {
let time = new Date().getTime() - new Date(response.headers.date).getTime()
wx.reportMonitor('1', +(time))
}
// 错误提示
if (response.status != 200) {
wx.showToast({
title: '出错啦!请稍后再试试哦~',
icon: 'none',
duration: 2000
})
}
return promise.resolve(response.data)
},
(err, promise) => {
wx.hideNavigationBarLoading()
return promise.resolve()
}
)
export default request
新建utils/api.js文件
export const baseUrlApi = 'http://:8080'//---开发调试环境
//export const baseUrlApi = 'https://test.mini.com'//---测试环境https
//export const baseUrlApi = 'https://product.mini.com'//---生产环境https
src下新建service文件夹
login-service.js,order-service.js
import { baseUrlApi } from '../utils/api'
import request from '../utils/request'
export default {
// 登录
wxLogin: (data) =>
request.post(`/store-miniApp-web/external/interface/wechat/wxLogin`, data, { baseURL: baseUrlApi }),
// 收藏
addCollect: (goodId, status) =>
request.get(`/store-miniApp-web/store/member/addCollect?goodId=${goodId}&status=${status}`,
null, {
baseURL: baseUrlApi
}),
}
接口请求的使用
import loginApi from "@/service/login-service";
methods: {
//-登录
clickLoginBtn() {
var data = {
phone: '',
password: "",
};
console.log("登录参数==", data);
loginApi.wxLogin(data).then(
data => {
if (!data) {
this.$toast(data.msg);
return;
}
if (data.code==0) {
console.log("登录成功", data);
}
},
err => {
}
);
},
//-收藏
collect() {
let isCollect = "1"; //1收藏 0取消
let goodId = "";
loginApi.addCollect(goodsId, isCollect).then(data => {
if (data.code != 0) {
console.log("收藏失败", data);
return;
}
if (isCollect == 1) {
this.$toast("取消成功");
} else {
this.$toast("收藏成功");
}
});
}
}
mapGetters
mapMutations
const store new Vuex.Store({
state: {
count: 0
},
mutations: {
increment(state,n){
state.count += n
}
}
})
new Vue({
el:"#app",
store,
computed: {
count() {
return store.state.count
}
},
methods: {
add() {
//传参
store.commit('increment',10)
}
}
})
mapMutations辅助函数的传参
//store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
//定义state,并将listName设置为一个空对象
const state = {
listName: {}
}
//定义mutations,可以传参,用于设置state里的listName
const mutations = {
set_listname: (state,value) => {
state.listName=value
}
}
//定义getters,用于获取出state里的对象
const getters = {
get_listname: state => {
return state.listName
}
}
export default new Vuex.Store({
getters,
state,
mutations
})
在vue实例中注册store
//main.js
import Vue from 'vue'
import App from './App'
import store from './store'
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
template: '<App/>',
components: { App }
})
Fly.js
一个支持所有JavaScript运行环境的基于Promise的、支持请求转发、强大的http请求库。可以让您在多个端上尽可能大限度的实现代码复用。
https://wendux.github.io/dist/#/doc/flyio/readme
vuex的定义
Vuex 是一个专门为 Vue.js 应用程序开发的状态管理模式
集中存储和管理应用的所有组件的状态
store(仓库),new Vue.store({…})
请点赞!因为你的鼓励是我写作的最大动力!
吹逼交流群:711613774
mpvue搭建小程序框架的更多相关文章
- 基于mpvue搭建小程序项目框架
简介: mpvue框架对于从没有接触过小程序又要尝试小程序开发的人员来说,无疑是目前最好的选择.mpvue从底层支持 Vue.js 语法和构建工具体系,同时再结合相关UI组件库,便可以高效的实现小程序 ...
- 美团小程序框架mpvue入门
mpvue 主要特性 使用 mpvue 开发小程序,你将在小程序技术体系的基础上获取到这样一些能力: 1. 彻底的组件化开发能力:提高代码复用性 2. 完整的 Vue.js 开发体验 3. 方便的 V ...
- 美团小程序框架mpvue蹲坑指南
美团小程序框架mpvue(花名:没朋友)蹲坑指南 第一次接触小程序大概是17年初,当时小程序刚刚内侧,当时就被各种限制折腾的死去活来的,单向绑定, 没有promise,请求数限制,包大小限制,各种反人 ...
- mpvue最佳实践 , 美团出的一个小程序框架
看手机微信,看到说美团出了1个小程序框架, mpvue 搜下来试试,看了网上的一个对比 ----------------- 以下为引用 我们对微信小程序.mpvue.WePY 这三个开发框架的主要能 ...
- 小程序框架MpVue踩坑日记(一)
小程序也做了几个小功能模块了,总觉得需要总结一下,踩坑什么的还是得记录一下啊. 好吧,其实是为了方便回顾 首先,说到小程序框架,大家都知道wepy,不过,我是没用过 美团开发团队到mpvue到是个实在 ...
- 美团小程序框架mpvue入门教程
mpvue是一个使用 Vue.js 开发小程序的前端框架.框架基于 Vue.js 核心,mpvue 修改了 Vue.js 的 runtime 和 compiler 实现,使其可以运行在小程序环境中,从 ...
- 使用mpvue开发小程序教程(二)
在上篇文章中,我们介绍了使用mpvue开发小程序所需要的一些开发环境的搭建,并创建了第一个mpvue小程序代码骨架并将其运行起来.在本文中,我们来研究熟悉一下mpvue项目的主要目录和文件结构. 在V ...
- 为什么选择MpVue进行小程序的开发
前言 mpvue是一款使用Vue.js开发微信小程序的前端框架.使用此框架,开发者将得到完整的 Vue.js 开发体验,同时为H5和小程序提供了代码复用的能力.如果想将 H5 项目改造为小程序,或开发 ...
- 学习笔记:mpvue开发小程序——入门
接下来可能要开发一个小程序,同事推荐使用mpvue,那么我提前熟悉下. 官网地址:http://mpvue.com/ 1.快速上手 http://mpvue.com/mpvue/quickstart/ ...
随机推荐
- 记录:拷贝gitblit里的项目使用git命令clone、pull、push等,出现一直在加载,卡住没反应的问题
俺想克隆别人gitblit里的其中一个版本库(俺在别人gitblit有权限) 懂得git的道友们,都应该知道克隆一个公共项目,随便找到,打开git终端,输入git clone 项目地址就行了 到了俺这 ...
- C# 文件类中 File ,FileInfo 类的主要区别
System.IO命名空间中提供的文件操作类有File和FileInfo,这两个类的功能基本相同,只是File是静态类,其中所有方法都是静态的,可以通过类名直接调用,不需要实例化.而FileInfo是 ...
- IIS配置文件的XML格式不正确 applicationHost.config崩溃
错误提示如图: 检查C:\Windows\System32\inetsrv\config目录下的applicationHost.config文件,备份一份. 可使用IIS提供的AppCmd.exe的r ...
- nlp-roadmap
nlp-roadmap https://github.com/graykode/nlp-roadmap nlp-roadmap is Natural Language Processing ROADM ...
- 【kubernetes】通过rancher2部署k8s
1. K8S相关介绍 十分钟带你理解Kubernetes核心概念 2. 部署rancher # 更新操作系统软件包 yum update -y # 删除历史容器及数据 docker rm -f $(d ...
- cal of easter egg
在Linux中用cal命令查看1752年9月,发现3到13日全都消失了……
- html相对字体
文章:使用 rem 设置文字大小 使用rem作为字体单位.
- Fiddler使用资料-整理
以下是一个博主写的一个系列. 随笔分类 - Fiddler 10.Fiddler中设置断点修改Response 摘要:当然Fiddler中也能修改Response 第一种:打开Fiddler 点击 ...
- Vue中mapMutations映射方法的问题
今天又被自己给蠢到,找了半天没发现问题.大家看下代码. mutation-types.js 里我新增了一个类型.INIT_CURRENTORDER export const GET_USERINFO ...
- lomback插件在日志管理方面的应用
由于现在使用日志可以省去在解决bug时候的很多麻烦, lomback为我们提供了很方便的打印日志的管理 @RunWith(SpringRunner.class) @SpringBootTest @Sl ...