<script src = "/path/to/vue.js"></script>
<script src = "/path/to/vue-router.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
npm install vue-router
git clone https://github.com/vuejs/vue-router.git node_modules/vue-router
cd node_modules/vue-router
npm install
npm run build
<html>
<head>
<title>Vue.js Routing</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id = "router_1">
<h2>This is Routing Example</h2>
<p>
<!-- use router-link component for navigation. -->
<!-- `<router-link>` will be rendered as an `<a>` tag by default -->
<router-link to = "/route1">Click on Router Link 1</router-link>
<router-link to = "/route2"> Click on Router Link 2</router-link>
</p>
<!-- route outlet -->
<!-- component matched by the route will render here -->
<router-view></router-view>
</div>
<script type = "text/javascript">
const Route1 = { template: '<div style = "border-radius:30px;background-color:lightpink;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;">You are seeing router link 1</div>' }
const Route2 = { template: '<div style = "border-radius:20px;background-color:lightgreen;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;"> You are seeing router link 2</div>' }
const routes = [
{ path: '/route1', component: Route1 },
{ path: '/route2', component: Route2 }
];
const router = new VueRouter({
routes // short for `routes: routes`
});
</script>
<script src="index.js"></script>
</body>
</html>
var vm = new Vue({
el: '#router_1',
router
})
html, body {
margin: 5px;
padding: 0;
}
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>4
<router-link to = "/route1">Click on Router Link 1</router-link>
<router-link to = "/route2"> Click on Router Link 2</router-link>
const Route1 = { template: '<div style = "border-radius:30px;background-color:lightpink;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;">You are seeing router link 1</div>' }
const Route2 = { template: '<div style = "border-radius:20px;background-color:lightgreen;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;"> You are seeing router link 2</div>' }
const routes = [
{ path: '/route1', component: Route1 },
{ path: '/route2', component: Route2 }
];
<router-link to = "name_of_-the_path"></router-link>
const router = new VueRouter({
routes // short for `routes: routes`
});
var vm = new Vue({
el: '#router_1',
router
})
<router-link to = "/route1">Click on Router Link 1</router-link>
<router-link to = "/route2"> Click on Router Link 2</router-link>
<router-link to = "/route1">Click on Router Link 1</router-link>
It is rendered as:
<a href = "#/route">Router Link </a>
<router-link v-bind:to = "{path:'/route1'}">Click on Router Link 2</router-link> // When using an object, it is recommended to bind it as shown in e.g. 2.
<router-link v-bind:to = "{path:'/route1', query: { name: 'Alex' }}">Click on Router Link 3</router-link>//You can pass a query string as shown in e.g. 3. This is an example of router link with query string.
<html>
<head>
<title>Vue.js Routing</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id = "router_1">
<h2>This is Routing Example</h2>
<p>
<!-- use router-link component for navigation. -->
<!-- `<router-link>` will be rendered as an `<a>` tag by default -->
<router-link v-bind:to = "{path:'/route2'}">Click on Router Link 2</router-link>
<router-link v-bind:to = "{path:'/route3', query: { name: 'Alex' }}">Click on Router Link 3</router-link>
</p>
<!-- route outlet -->
<!-- component matched by the route will render here -->
<router-view></router-view>
</div>
<script type = "text/javascript">
const Route2 = { template: '<div style = "border-radius:30px;background-color:lightpink;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;">You are seeing router link 2</div>' }
const Route3 = { template: '<div style = "border-radius:20px;background-color:lightgreen;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;"> You are seeing router link 3</div>' }
const routes = [
{ path: '/route2', component: Route2 },
{ path: '/route3', component: Route3 }
];
const router = new VueRouter({
routes // short for `routes: routes`
});
</script>
<script src="index.js"></script>
</body>
</html>
var vm = new Vue({
el: '#router_1',
router
})
<html>
<head>
<title>Vue.js Routing</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id = "router_1">
<h2>This is Routing Example</h2>
<p>
<!-- use router-link component for navigation. -->
<!-- `<router-link>` will be rendered as an `<a>` tag by default -->
<router-link v-bind:to = "{path:'/route2'}">Click on Router Link 2</router-link>
<router-link v-bind:to = "{path:'/route3', query: { name: 'Panda' }}" replace>This link is Replaced</router-link>
</p>
<!-- route outlet -->
<!-- component matched by the route will render here -->
<router-view></router-view>
</div>
<script type = "text/javascript">
const Route2 = { template: '<div style = "border-radius:30px;background-color:lightpink;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;">You are seeing router link 2</div>' }
const Route3 = { template: '<div style = "border-radius:20px;background-color:lightgreen;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;"> You are seeing router link 3</div>' }
const routes = [
{ path: '/route2', component: Route2 },
{ path: '/route3', component: Route3 }
];
const router = new VueRouter({
routes // short for `routes: routes`
});
</script>
<script src="index.js"></script>
</body>
</html>
<html>
<head>
<title>Vue.js Routing</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id = "router_1">
<h2>This is Routing Example</h2>
<p>
<!-- use router-link component for navigation. -->
<!-- `<router-link>` will be rendered as an `<a>` tag by default -->
<router-link v-bind:to = "{path:'/route2'}">Click on Router Link 2</router-link>
<router-link v-bind:to = "{ path: '/route3'}" append>This is an appended link</router-link>
</p>
<!-- route outlet -->
<!-- component matched by the route will render here -->
<router-view></router-view>
</div>
<script type = "text/javascript">
const Route2 = { template: '<div style = "border-radius:30px;background-color:lightpink;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;">You are seeing router link 2</div>' }
const Route3 = { template: '<div style = "border-radius:20px;background-color:lightgreen;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;"> You are seeing the appended link router</div>' }
const routes = [
{ path: '/route2', component: Route2 },
{ path: '/route3', component: Route3 }
];
const router = new VueRouter({
routes // short for `routes: routes`
});
</script>
<script src="index.js"></script>
</body>
</html>
var vm = new Vue({
el: '#router_1',
router
})
html, body {
margin: 5px;
padding: 0;
}
<html>
<head>
<title>Vue.js Routing</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id = "router_1">
<h2>This is Routing Example</h2>
<p>
<router-link v-bind:to = "{ path: '/route2'}" tag = "span">Click on Router Link 2</router-link>
<router-link v-bind:to = "{ path: '/route3'}" tag = "span">Click on Router Link 3</router-link>
</p>
<!-- route outlet -->
<!-- component matched by the route will render here -->
<router-view></router-view>
</div>
<script type = "text/javascript">
const Route2 = { template: '<div style = "border-radius:30px;background-color:lightpink;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;">You are seeing router link 2</div>' }
const Route3 = { template: '<div style = "border-radius:20px;background-color:lightgreen;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;"> You are seeing router link 3</div>' }
const routes = [
{ path: '/route2', component: Route2 },
{ path: '/route3', component: Route3 }
];
const router = new VueRouter({
routes // short for `routes: routes`
});
</script>
<script src="index.js"></script>
</body>
</html>
<style>
._active{
background-color : red;
}
</style>
<p>
<router-link v-bind:to = "{ path: '/route2'}" active-class = "._active">Click on Router Link 2</router-link>
<router-link v-bind:to = "{ path: '/route3'}" tag = "span">Click on Router Link 3</router-link>
</p>
<style>
._active{
background-color : red;
}
</style>
<p>
<router-link v-bind:to = "{ path: '/route2'}" exact-active-class = "._active">Click on Router Link 2</router-link>
<router-link v-bind:to = "{ path: '/route3'}" tag = "span">Click on Router Link 3</router-link>
</p>
<html>
<head>
<title>Vue.js Routing</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id = "router_1">
<h2>This is Routing Example</h2>
<style>
._active{
background-color : red;
}
</style>
<p>
<router-link v-bind:to = "{ path: '/route2'}" event = "mouseover">Hover this link</router-link>
<router-link v-bind:to = "{ path: '/route3'}" tag = "span">Click on Router Link 3</router-link>
</p>
<!-- route outlet -->
<!-- component matched by the route will render here -->
<router-view></router-view>
</div>
<script type = "text/javascript">
const Route2 = { template: '<div style = "border-radius:30px;background-color:lightpink;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;">You are seeing router link 2</div>' }
const Route3 = { template: '<div style = "border-radius:20px;background-color:lightgreen;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;"> You are seeing router link 3</div>' }
const routes = [
{ path: '/route2', component: Route2 },
{ path: '/route3', component: Route3 }
];
const router = new VueRouter({
routes // short for `routes: routes`
});
</script>
<script src="index.js"></script>
</body>
</html>