Skip to content

Commit

Permalink
vue-router vuex模块
Browse files Browse the repository at this point in the history
  • Loading branch information
Ljhhhhhh committed Oct 8, 2019
1 parent 87498d8 commit c9878c3
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 119 deletions.
12 changes: 12 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
module.exports = {
presets: [
'@vue/app'
],
plugins: [
['import', {
libraryName: 'vant',
libraryDirectory: 'es',
style: true
}, 'vant']
]
}
/*
'postcss-pxtorem': {
rootValue: 37.5,
propList: ['*']
} */
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"lint": "vue-cli-service lint"
},
"dependencies": {
"axios": "^0.19.0",
"core-js": "^2.6.5",
"lib-flexible": "^0.3.2",
"vant": "^2.2.5",
"vue": "^2.6.10",
"vue-router": "^3.0.3",
"vuex": "^3.0.1"
Expand All @@ -18,9 +21,12 @@
"@vue/cli-plugin-eslint": "^3.11.0",
"@vue/cli-service": "^3.11.0",
"@vue/eslint-config-standard": "^4.0.0",
"amfe-flexible": "^2.2.1",
"babel-eslint": "^10.0.1",
"babel-plugin-import": "^1.12.2",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"postcss-pxtorem": "^4.0.1",
"sass": "^1.18.0",
"sass-loader": "^7.1.0",
"vue-template-compiler": "^2.6.10"
Expand Down
8 changes: 7 additions & 1 deletion postcss.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module.exports = {
plugins: {
autoprefixer: {}
autoprefixer: {
overrideBrowserslist: ['Android >= 4.0', 'iOS >= 7']
},
'postcss-pxtorem': {
rootValue: 75,
propList: ['*']
}
}
}
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<!-- <meta name="viewport" content="width=device-width,initial-scale=1.0"> -->
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title>vue-cli-h5</title>
</head>
Expand Down
14 changes: 2 additions & 12 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<template>
<div id="app">
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
<router-link to="/?id=3">Home</router-link> |
<router-link to="/about?id=1">About</router-link>
</div>
<router-view/>
</div>
Expand All @@ -16,14 +16,4 @@
text-align: center;
color: #2c3e50;
}
#nav {
padding: 30px;
a {
font-weight: bold;
color: #2c3e50;
&.router-link-exact-active {
color: #42b983;
}
}
}
</style>
58 changes: 0 additions & 58 deletions src/components/HelloWorld.vue

This file was deleted.

1 change: 1 addition & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vue from 'vue'
import 'lib-flexible'
import App from './App.vue'
import router from './router'
import store from './store'
Expand Down
25 changes: 0 additions & 25 deletions src/router.js

This file was deleted.

7 changes: 7 additions & 0 deletions src/router/about.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default [
{
path: '/about',
name: 'about',
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
}
]
56 changes: 56 additions & 0 deletions src/router/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Vue from 'vue'
import Router from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(Router)

let routes = [
{
path: '/',
name: 'home',
component: Home
}
]

const routerContext = require.context('./', true, /\.js$/)
routerContext.keys().forEach(route => {
// 如果是根目录的 index.js 、不处理
if (route.startsWith('./index')) {
return
}
const routerModule = routerContext(route)
/**
* 兼容 import export 和 require module.export 两种规范
*/
routes = routes.concat(routerModule.default || routerModule)
})
const myRouter = new Router({
mode: 'history',
base: process.env.BASE_URL,
routes
})

myRouter.beforeEach((to, from, next) => {
if (to.params.direction) {
store.commit('updateDirection', to.params.direction)
} else {
const toIndex = history.getItem(to.path)
const fromIndex = history.getItem(from.path)
// 判断并记录跳转页面是否访问过,以此判断跳转过渡方式
if (toIndex) {
if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
store.commit('updateDirection', 'forward')
} else {
store.commit('updateDirection', 'back')
}
} else {
++historyCount
history.setItem('count', historyCount)
to.path !== '/' && history.setItem(to.path, historyCount)
store.commit('updateDirection', 'forward')
}
}
next()
})

export default myRouter
16 changes: 0 additions & 16 deletions src/store.js

This file was deleted.

37 changes: 37 additions & 0 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Vue from 'vue'
import Vuex from 'vuex'
import test from './modules/test'

Vue.use(Vuex)

export default new Vuex.Store({
state: {
msg: 'hello vuex',
loading: false
},
getters: {
loading (state) {
return state.loading
}
},
mutations: {
setMsg (state, msg) {
state.msg = msg
},
setLoading (state, val) {
state.loading = val
}
},
actions: {

},
modules: {
test
}
})

/*
this.$store.registerModule('d',{
})
*/
28 changes: 28 additions & 0 deletions src/store/modules/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const state = {
number: 1
}
const actions = {
onePlusAction: async ({ commit }, { val }) => {
commit('setLoading', true, { root: true })
// loading状态
await setTimeout(() => {
commit('onePlus', val)
commit('setLoading', false, { root: true })
}, 1500)
}
}
const mutations = {
onePlus (state, val = 1) {
state.number = state.number + val
}
}
const getters = {

}
export default {
namespaced: true,
state,
actions,
mutations,
getters
}
4 changes: 4 additions & 0 deletions src/views/About.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
<h1>This is an about page</h1>
</div>
</template>
<script>
export default {
}
</script>
Loading

0 comments on commit c9878c3

Please sign in to comment.