Git Product home page Git Product logo

mpvue-new's Introduction

根据官方的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'
    ])
  }
}

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.