-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb76f83
commit 835bdbb
Showing
7 changed files
with
158 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,8 +196,86 @@ js 部分: | |
|
||
npm install [email protected] --save | ||
|
||
然后就可以创建几个页面来使用路由了,下面是几处关键代码: | ||
|
||
html: | ||
|
||
<div id="app"> | ||
<router-view></router-view> | ||
</div> | ||
|
||
index.js | ||
|
||
var Vue = require('vue'); | ||
var VueRouter = require('vue-router'); | ||
var routerConfig = require('./router-config'); | ||
|
||
var router = new VueRouter({ | ||
transitionOnLoad: false, | ||
routes: routerConfig | ||
}); | ||
|
||
Vue.use(VueRouter); | ||
|
||
new Vue({ | ||
router: router | ||
}).$mount('#app'); | ||
|
||
其中用到的 `router-config` 配置文件 `router-config.js` 的代码: | ||
|
||
var PageA = require('./page/page-a'); | ||
var PageB = require('./page/page-b'); | ||
|
||
module.exports = [ | ||
{ | ||
path: '/', | ||
component: PageA | ||
}, | ||
{ | ||
name: 'pageA', | ||
path: '/page/a', | ||
component: PageA | ||
}, | ||
{ | ||
name: 'pageB', | ||
path: '/page/b', | ||
component: PageB | ||
} | ||
]; | ||
|
||
其中页面 `./page/page-a` 的代码 `./page/page-a/index.vue`: | ||
|
||
<template> | ||
<div> | ||
当前页面:Page A | ||
<div> | ||
<router-link to="/page/b"> 跳转到 Page B </router-link> | ||
</div> | ||
</div> | ||
</template> | ||
<script> | ||
module.exports = { | ||
data: function () { | ||
return {}; | ||
} | ||
}; | ||
</script> | ||
<style> | ||
div { | ||
padding: 20px; | ||
text-align: center; | ||
color: red; | ||
} | ||
</style> | ||
|
||
其中 `Vue.use(VueRouter)` 容易被忽略,官网有这么一句話: | ||
|
||
0. If using a module system (e.g. via vue-cli), import Vue and VueRouter and then call Vue.use(VueRouter). | ||
|
||
如果使用模块化机制编程,需要调用 `Vue.use(VueRouter)`。 | ||
|
||
## 参考 | ||
|
||
[Standalone-vs-Runtime-only-Build](https://vuejs.org/v2/guide/installation.html#Standalone-vs-Runtime-only-Build) | ||
|
||
|
||
[vue router getting started](http://router.vuejs.org/en/essentials/getting-started.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<template> | ||
<div> | ||
当前页面:Page A | ||
<div> | ||
<router-link to="/page/b"> 跳转到 Page B </router-link> | ||
</div> | ||
</div> | ||
</template> | ||
<script> | ||
module.exports = { | ||
data: function () { | ||
return {}; | ||
} | ||
}; | ||
</script> | ||
<style> | ||
div { | ||
padding: 20px; | ||
text-align: center; | ||
color: red; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
<template> | ||
<div> | ||
Page B | ||
</div> | ||
</template> | ||
<script> | ||
module.exports = { | ||
data: function () { | ||
return {}; | ||
} | ||
}; | ||
</script> | ||
<style> | ||
div { | ||
padding: 20px; | ||
text-align: center; | ||
color: red; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* 路由配置文件 | ||
* | ||
* Created by zhaoxiaoqiang on 2016/12/11. | ||
*/ | ||
|
||
var PageA = require('./page/page-a'); | ||
var PageB = require('./page/page-b'); | ||
|
||
module.exports = [ | ||
{ | ||
path: '/', | ||
component: PageA | ||
}, | ||
{ | ||
name: 'pageA', | ||
path: '/page/a', | ||
component: PageA | ||
}, | ||
{ | ||
name: 'pageB', | ||
path: '/page/b', | ||
component: PageB | ||
} | ||
]; |