Vue – Router

Set up

To use vue-router, previous easy html does not work properly.
I set up simple environment using npm. Please follow

Install vue and vue-router

npm init 
npm install vue vue-router
npm install http-server -D

http-server is to run server locally. (It’s easy to try route)
To run http-server easily, I changed a bit of npm command.

  "name": "sample6",
  "version": "1.0.0",
  "description": "Vue Router Sample",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "http-server -o"
  "keywords": [
  "author": "DJ110",
  "license": "MIT",
  "dependencies": {
    "vue": "^2.5.13",
    "vue-router": "^3.0.1"
  "devDependencies": {
    "http-server": "^0.10.0"

Hash mode and HTML5 mode

This is same as Angular.
By default, vue router is hash mode.

Mode Description
Hash mode Add # tag under root URL
HTML5 No hash tag, normal type

In this sample, I use HTML5 mode (not hash mode) to make URL simple.
We can set by code.

Sample Code

    <script src="node_modules/vue/dist/vue.min.js"></script>
    <script src="node_modules/vue-router/dist/vue-router.min.js"></script>
    <div id="app">
      <router-link to="/top">TopPage</router-link>
      <router-link to="/users">User List</router-link>
      <router-link :to="{ name: 'user', params: { userId: 123}}">User Details</router-link>
    var router = new VueRouter({
      mode: 'history',  <!-- If delete, this is hash mode -->
      routes: [
          path: '/top',
          component: {
            template: '<div>Top page</div>'
          path: '/users',
          component: {
            template: '<div>User List</div>'
          path: '/user/:userId',
          name: 'user',
          component: {
            template: '<div>User Id: {{ $route.params.userId}}</div>'

    var app = new Vue({
      router: router


router-view is changeable layout part.