Simple Vue Router Example From Scratch

Today, We want to share with you Simple Vue Router Example From Scratch.In this post we will show you Vue.js Routing With vue-router, hear for Introduction to Routing in Vue.js with vue-router we will give you demo and example for implement.In this post, we will learn about Vue Router Tutorial With Example – How To Use Routing in VueJS with an example.

Simple Vue Router Example From Scratch

There are the Following The simple About Simple Vue Router Example From Scratch Full Information With Example and source code.

Read Also:  Vue JS - Get Query String Value Example

As I will cover this Post with live Working example to develop Laravel Vue Router Example From Scratch, so the vue router default route Directory structures for this example is following below.

vuejs/vue-router Example

Step 1: External Libs

Include the library in the page

<script src="https://unpkg.com/vue/dist/vue.min.js"></script>
<script src="https://unpkg.com/vue-router"></script>

Step 2: Add Vuejs JavaScript Part

console.clear()
console.log('Yes! We are using Vue version', Vue.version)

Vue.use(VueRouter)

const Dashboard = {
	template: `<h1>Dashboard</h1>`,
}

const Product = {
	template: `<h1>My Product</h1>`,
}

const Product = {
	template: `<h1>Product</h1>`,
}

const routes = [
	{ path: '/', name: 'dashboard', component: Dashboard },
  { path: '/shop', name: 'shop', component: Product },
  { path: '/product', name: 'product', component: Product },
]

const router = new VueRouter({
	routes,
})

// New VueJS instance
var app = new Vue({
	// CSS selector of the root DOM element
  el: '#app',
  // Inject the router into the app
  router,
})

Step 3: Include HTML

<div id="app">
  <nav>
    <router-link :to="{ name: 'dashboard' }" exact>Dashboard</router-link>
    <router-link :to="{ name: 'shop' }">Shop</router-link>
    <router-link :to="{ name: 'product' }">Product</router-link>
  </nav>
  
  <router-view></router-view>
</div>

Step 4: Include CSS

body {
  font-family: sans-serif;
}

a {
  color: #41b883;
  text-decoration: none;
}

a.router-link-active {
  font-weight: bold;
  text-decoration: underline;
}

Web Programming Tutorials Example with Demo

Read :

Read Also:  How to get current url with or without query string using Vuejs

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Simple Vue Router Example From Scratch.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.