Skip to content

Commit b15ea09

Browse files
committed
add: 增加vue-router对应mode:history模式的实例🚀
1 parent 3af1df8 commit b15ea09

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

controller/history.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
/**
4+
* ['/history/page']
5+
* The path used by history mode
6+
*/
7+
exports.page = function*() {
8+
yield this.bindDefault()
9+
10+
yield this.render('history', {
11+
siteInfo: this.siteInfo
12+
})
13+
}
14+
/**
15+
* Match all path (begins with '/history/page/').
16+
* Such as: /history/page/:any-words/:any-times/...
17+
*/
18+
exports.page.__regular__ = /^\/history\/page\/(?:.*)(?:\/(?=$))?$/
19+
20+
/**
21+
* ['/history' & '/history/page']
22+
* Make the index path redirect to history path automatically
23+
*/
24+
exports.index = exports.page
25+
26+
/**
27+
* ['/history/ajax']
28+
* Other paths to do other things
29+
*/
30+
exports.ajax = {
31+
demo: function*() {
32+
yield this.proxy(`http://${this.host}/ajax/test/1`)
33+
}
34+
}

vues/history/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// The Vue build version to load with the `import` command
2+
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
3+
import Vue from 'vue'
4+
import VueRouter from 'vue-router'
5+
import VueResource from 'vue-resource'
6+
import routes from './router.js'
7+
import App from './index.vue'
8+
9+
// 初始化路由
10+
/* eslint-disable no-new */
11+
const router = new VueRouter(routes)
12+
13+
// 置入组件
14+
Vue.use(VueRouter)
15+
Vue.use(VueResource)
16+
17+
new Vue({
18+
/**
19+
* 提供的元素只能作为挂载点。
20+
* 不同于 Vue 1.x,所有的挂载元素会被 Vue 生成的 DOM 替换。
21+
* 因此不推荐挂载root实例到 <html> 或者 <body> 上。
22+
*/
23+
el: '#app',
24+
template: '<App/>',
25+
components: { App },
26+
/**
27+
* 置入路由
28+
*/
29+
router
30+
})
31+
/* eslint-enable no-new */

vues/history/index.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<div id="history">
3+
<router-view></router-view>
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
name: 'history'
10+
}
11+
</script>
12+
13+
<style lang="less">
14+
#history{
15+
/* some style */
16+
}
17+
</style>

vues/history/router.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const prefix = '/history/page'
2+
3+
function getDemoHtml (path) {
4+
return `<div>
5+
<a href="${prefix}${path}">
6+
&lt;a&gt; will Reload-The-Page as usual!
7+
</a>
8+
<br>
9+
<router-link to="${prefix}${path}">
10+
&lt;router-link&gt; use real History api!
11+
</router-link>
12+
<span style="color: red;">[use this!]</span>
13+
</div>`
14+
}
15+
16+
module.exports = {
17+
mode: 'history',
18+
routes: [{
19+
path: `${prefix}/index`,
20+
component: {
21+
template: getDemoHtml('/list')
22+
}
23+
}, {
24+
path: `${prefix}/list`,
25+
component: {
26+
template: getDemoHtml('/test')
27+
}
28+
}, {
29+
path: `${prefix}/test`,
30+
component: {
31+
template: getDemoHtml(`/test/${Math.random().toString(36).substring(2)}`)
32+
}
33+
}, {
34+
path: `${prefix}/test/:test`,
35+
component: {
36+
template: getDemoHtml('/index')
37+
}
38+
}, {
39+
path: '*',
40+
redirect: `${prefix}/index`
41+
}]
42+
}

0 commit comments

Comments
 (0)