Skip to content

ivanlee93/mpvue-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

感谢 @spencer1994 @F-loat 大神们分享的源码

基本用法

$ npm install -g vue-cli
$ vue init ivanlee93/mpvue-cli mpvuesimple
$ cd mpvuesimple
$ npm install
$ npm run dev

解决 #140

$ vue init ivanlee93/mpvue-cli-fix mpvuesimple(解决mpvue多个页面公用一个vm对象的问题)

注意:SourceMap 映射问题,webpack-mpvue-asset-plugin 降级到0.1.1即可解决

纯微信小程序推荐omi-mp-create,腾讯出品的原生框架,有类似Vuex的体验,新手使用mpvue,不建议用在公司的紧急上线的项目上!

uni-App 也是一个不错跨平台框架

优化官方的脚手架,整合优秀开源插件,使开发风格更接近Vue/Vuex

flyio 同时支持浏览器、小程序、Node、Weex的基于Promise的跨平台http请求库。可以让您在多个端上尽可能大限度的实现代码复用。

minapp-api-promise 将所有异步微信小程序API promise化,支持then/catch、async/await的方式调用小程序API;支持请求队列,支持对原生API进行拦截。

mpvue-entry 集中式页面配置,避免重复编辑各页面的 main.js 文件,优化目录结构。

mpvue-router-patch 在 mpvue 中使用 vue-router 兼容的路由写法。

根据官方的cli封装了一系列的开发基础

感谢@spencer1994,在他开源的源码基础上进行增强并整合到最新的mpvue上面

主要的开发便利包含如下:

  1. 使用了mpvue-entry

优点:去除了各个子页面的main.js,创建了pages.js,集中式页面配置,使开发更贴近vue风格。

[2018-05-24] 更新了mpvue-entry的版本=>1.1.7,支持热更新,不需要重启。

[2018-09-07] 更新了v1.5.x 版本开始支持 mpvue-loader@^1.1.0 版本

[2019-01-09] 更新了v2.x 版本仅支持 mpvue-loader@^1.1.0,兼容 megalo

  1. 使用flyio并封装了请求,

优点:根据vuex官方推荐,将background API封装到actions中,具体用法可在代码里查看。

  1. 使用mpvue-router-patch

优点:页面跳转直接可以用 vue-router 兼容的路由写法,开发体验更加接近Vue原生体验。

  1. 使用minapp-api-promise

优点:将所有异步微信小程序API promise化,支持then/catch、async/await的方式调用小程序API,具体用法可在代码里查看。

  1. 在 App.vue 页面中添加一些小程序页面快速布局的样式类

优点:使用flex简单高效快速布局,并且兼容多设备端

  1. 封装了auth

优点:简化了授权的代码量,点击按钮的时候自动判断授权,如果没有自动弹窗

// 在需要授权(比如地理位置,保存图片)的地方this.$iBox.auth('权限名',{权限参数},'弹窗的提示')。
// 比如写了一个保存海报的功能,最后执行saveImageToPhotosAlbum这个方法即可,无需再判断用户取消了保存权限的逻辑
saveImageToPhotosAlbum (e) {
      this.$iBox.auth(
        'saveImageToPhotosAlbum', {
          filePath: e.target.tempFilePath,
          success: (res) => {
            this.$iBox.showToast('保存成功')
            this.isSaveImage = true
          },
          fail: (res) => {
            wx.hideLoading()
          }
        },
        '点击确定获取保存相册的权限'
      )
}
  1. 自动注册store

优点:多人协作开发不需要担心代码冲突,不需要每个store.js都要import引入。

以下是关于第7点的说明:

根据webpack的require.context及store的registerModule方法来自动注册store的modules

在src下增加store文件夹。具体目录如下

  |__src
    |__store
      |__modlues
        |__counter.js
        |__demo.js
      |__index.js

index.js的代码如下:

// https://vuex.vuejs.org/zh-cn/intro.html
// make sure to call Vue.use(Vuex) if using a module system
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({})

const storeContext = require.context('@/store/modules', true, /\.js$/)

storeContext.keys().forEach((modules) => {
  store.registerModule(modules.replace(/(^\.\/)|(\.js$)/g, ''), storeContext(modules).default)
})

Vue.prototype.$store = store
export default store

src/main.js代码如下:

import Vue from 'vue'
import App from '@/App'
import store from '@/store'
import IboxPlugin from '@/plugins/ibox'
import WXP from 'minapp-api-promise'
import MpvueRouterPatch from 'mpvue-router-patch'

Vue.use(IboxPlugin)
Vue.use(MpvueRouterPatch)
Vue.config.productionTip = false
Vue.prototype.$wx = WXP
App.store = store
App.mpType = 'app'

const app = new Vue(App)
app.$mount()

在页面中使用如下

在单独的页面store.js中增加了namespaced:true。需要根据文件名来区分state及commit,这样子不同的store中的方法重名也不需要担心出错了。具体使用可以加actions,使用vuex的mapState、mapActions辅助函数方便使用。

import { mapState, mapActions } from 'vuex'
export default {
  computed: {
    ...mapState({
      count: state => state.counter.count
    })
  },
  methods: {
   ...mapActions('counter', [
      'increment',
      'decrement'
    ])
  }
}

目录结构

|____build              webpack打包的环境代码
|____config             webpack打包的配置文件
|____node_modules       项目运行依赖的npm包
|____src                项目代码文件夹
 |__components          自定义组件
 |__packageA            分包页面组件
 |__pages               页面组件
 |__plugins             vue插件
  |__ibox
   |__index.js          vue插件的注册,包含接口请求及工具utils
   |__utils.js          工具类及共用方法注册js
  |__flyio          
   |__apiUrl            接口请求地址管理
   |__config            接口请求配置管理
   |__interceptors.js   接口请求拦截器
   |__request           接口请求封装(包括loading及toast,接口的定制化配置及默认配置)
  |__modules            store的管理文件
  |__index.js           实现store对modules文件下的自动注册
 |__store               vuex状态管理
 |__app.json            小程序app.json配置
 |__App.vue             小程序的App页面【整合了小程序页面快速布局的一些样式类】
 |__main.js             类似vue的main.js,可以插件进行配置
 |__pages.js            小程序的page.json的配置 集中式页面配置
|____static             静态资源文件夹
|____.babelrc           es6语法转换配置文件
|____.editorconfig      编辑器配置
|____.eslintignore      eslint的忽略配置
|____.eslintrc.js       eslint配置
|____.gitignore         git push忽略配置
|____.postcssrc.js      postcss插件的配置文件
|____index.html         SPA的index页面
|____package.json       npm包配置文件
|____README.md          readme文档

整合后的稳定性如何?

可以放心使用,几个整合的插件都是官方推荐的,并且经过了自己的测试,上线了几个小程序,运行稳定。

1.config.js中配置与后台规定的表示响应成功的变量(或注释掉相应代码片段),否则会导致接口返回异常。

2.vue文件中不能缺少script标签,否则会导致编译不了。

3.每个页面都要适配iphoneX,padding-bottom: 34px。可参考其他页面实现方式。注:底部无操作的话就不用将页面顶上去。

4.slot插槽数据渲染有问题 Meituan-Dianping/mpvue#427

5.页面初始化的data方式 Object.assign(this.$data, this.$options.data())

About

对mpvue官方quick-start的模版进行优化改造,更新

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy