Vue.js Routing
In this chapter we will introduce Vue.js routing.
Vue.js routing allows us to access different content via different URLs.
With Vue.js you can build a multi-view single page web application (SPA).
Vue.js routing requires loading the vue-router library
Chinese documentation address:vue-router documentation.
Installation
1. Direct download / CDN
https://unpkg.com/vue-router/dist/vue-router.js
NPM
It is recommended to use the Taobao mirror:
cnpm install vue-router
Simple Example
Vue.js + vue-router makes it easy to implement a single-page application.
In the example below we add vue-router, then configure the components and route mappings, and tell vue-router where to render them. The code is as follows:
HTML Code
<script src="https://unpkg.com/vue/dist/vue.js" script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js" script>
<div id="app">
<h1>Hello App!
h1
<
p
<
router-link
to
=
"
/foo
"Go to Foo
router-link
<
router-link
to
=
"
/bar
"Go to Bar
router-link
p
<
router-view
router-view
div
//
//
//Can be imported from other files
const
Foo
=
{
template
:
'
'
}
const
Bar
=
{
template
:
'
'
}
//
- Define routes
//
Each route should map to a component. Here "component" can be
//
a component constructor created via Vue.extend(),
//
or simply a component options object.
//
We will discuss nested routes later.
const
routes
=
[
{
path
:
'
/foo
'
,
component
:
Foo
}
,
{
path
:
'
/bar
'
,
component
:
Bar
}
]
//
- Create the router instance, then pass
routesConfigure
//
You can also pass other configuration options, but let's keep it simple for now.
const
router
=
new
VueRouter
(
{
routes
//
(shorthand) equivalent to routes: routes
}
)
//
- Create and mount the root instance.
//
Remember to inject the router via the router configuration option,
//
so that the whole app has routing capability
const
app
=
new
Vue
(
{
router
}
)
.$
mount
(
'
#app
'
)
//
Now, the application is up and running!
Try it »
Clicked navigation links will get the style class "router-link-exact-active router-link-active".
Related Attributes
Next we can learn more about
to
represents the link to the target route. When clicked, it immediately passes the value of to to router.push(), so this value can be a string or an object describing the target location.
Home
Home
Home
Home
Home
User
Register
replace
If the replace attribute is set, clicking it calls router.replace() instead of router.push(), and no history record is left after navigation.
append
After setting the append attribute, its path is added before the current (relative) path. For example, if we navigate from /a to a relative path b, without append the path is /b, and with it the path is /a/b
tag
Sometimes you want to render as a certain tag, such as . So we use tag 's prop to specify which tag, and it still listens for clicks to trigger navigation.
foo
foo
active-class
Sets the CSS class name used when the link is active. It can be replaced with the following code.
Router Link 1
Router Link 2
Note here that class uses active-class="_active".
exact-active-class
Configures the class to activate when the link is exactly matched. It can be replaced with the following code.
Router Link 1
Router Link 2
event
Declares the events that can be used to trigger navigation. Can be a string or an array of strings.
Router Link 1
The code above sets event to mouseover, so when the mouse moves over Router Link 1, the navigated HTML content changes.
NPM routing example
Next we demonstrate a simple routing example using npm. Before starting, please download the example source code:
Routing example
You can also download it on Github:https://github.com/chrisvfritz/vue-2.0-simple-routing-example
After downloading, extract the directory, rename it to vue-demo, vu and enter the directory, and run the following commands:
# Install dependencies, using Taobao resource command cnpm
cnpm install
# Start application, address is localhost:8080
cnpm run dev
If you need to publish to a production environment, run the following commands:
cnpm run build
After it succeeds, visit http://localhost:8080 to see the following interface:
