Giter Club home page Giter Club logo

vue-chunkroutesstore's Introduction

因为webpack的require.context的第一个参数只支持字面量,无法使用变量,所以本项目的路径是写死的,具体路径可以在注册时修改,或者在webpack的alias配置中添加别称。

  • 路由的路径为~/routes,在webpack的alias别称中添加了~来代替src~/routes即是src下的routes文件夹。

  • 同理~/store/modules即为src/store/modules文件夹。

使用了这个插件后。

不需要在routes/index.js中写routes,会自动检索src/routes/modules下的所有除了index.js的js文件,自动注册到路由中。

不需要在store/index.js中写入modules,会自动检索src/store/modules下所有的js文件,自动注册到store中。

使用方法

import store from '~/store'
import router from '~/routes'

import autoRouteStore from './index'
const routerContext = require.context('~/routes/modules', true, /\.js$/)
const storeContext = require.context('~/store/modules', true, /\.js$/)
Vue.use(autoRouteStore, {
  router,
  routerContext,
  storeContext,
  store
})

注意事项

因webpack在dev下的缓存配置问题,当删除目录下的文件夹时,需要npm run dev重新启动项目来更新缓存

目录

|__src
  |__routes
    |__modules
      |__demo1-router.js
      |__demo2-router.js
    |__index.js
  |__store
    |__modules
      |__demo1-store.js
      |__demo2-store.js
    |__global.js
    |__index.js

routes/index.js

import Vue from 'vue'   // 引入vue
import VueRouter from 'vue-router'   // 引入vue-router

const router = new VueRouter({
  routes: []
})

Vue.use(VueRouter)

export default router

store/index.js

import Vue from 'vue'
import Vuex from 'vuex'

import global from '~/store/global'
Vue.use(Vuex)

const store = new Vuex.Store({
  modules: {
    global
  }
})

export default store

vue-chunkroutesstore's People

Contributors

spencer1994 avatar

Watchers

 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.