Giter Club home page Giter Club logo

mpvue-cli's Introduction

感谢@blackjack0v0贡献的vue init的使用方式~

基本用法

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

目录结构

|____build              webpack打包的环境代码
|____config             webpack打包的配置文件
|____node_modules       项目运行依赖的npm包
|____src                项目代码文件夹
 |__components          自定义组件
 |__pages               页面组件
 |__plugins             vue插件
  |__ibox
   |__index.js          vue插件的注册,包含接口请求及工具utils
   |__utils.js          工具类及共用方法注册js
 |__router              小程序的page.json的配置
  |__flyio          
   |__apiUrl            接口请求地址管理
   |__config            接口请求配置管理
   |__interceptors.js   接口请求拦截器
   |__request           接口请求封装(包括loading及toast,接口的定制化配置及默认配置)
  |__modules            store的管理文件
  |__index.js           实现store对modules文件下的自动注册
 |__store               vuex状态管理
 |__App.vue             小程序的App页面
 |__main.js             小程序app.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文档

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

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

  1. 使用了mpvue-entry

优点:去除了各个子页面的main.js,创建了router文件夹,使开发更贴近vue风格。

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

缺陷:每新增一个页面都需要重新npm run dev,官方文档有说明原因。

【在2018-05-24通过更新mpvue-entry的版本解决了此缺陷】

  1. 自动注册store

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

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

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

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

根据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'

Vue.config.productionTip = false

import IboxPlugin from '@/plugins/ibox'
Vue.use(IboxPlugin)

const app = new Vue({
  store,
  ...App
})

app.$mount()

export default {
  config: {
    pages: [],
    window: {
      backgroundTextStyle: 'light',
      navigationBarBackgroundColor: '#fff',
      backgroundColor: '#ffffff',
      navigationBarTitleText: 'WeChat',
      navigationBarTextStyle: 'black'
    }
  }
}

在页面中使用如下

在单独的页面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'
    ])
  }
}

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

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

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

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

5.mpvue-loader更新到1.1.x更改了入口后,运行本项目会有问题,暂定将mpvue-loader锁定在1.0.14版本。等后续有时间再来优化这个东西。

mpvue-cli's People

Contributors

spencer1994 avatar

Watchers

James Cloos avatar SL avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.