Vue routing tutorial with mode history. by default in vuejs mode is hash(#) mode. To remove hash(#) from URL we use history mode in vuejs routing. After add mode: history in the router URL looks normal http://www.codermen.com/articles/submit To install vuejs use this flowing command. (you can skip this installation if you install vuejs CLI) After that, Now create a Vue.js project using the following command. After successfully creating a new project install VueJs routing using this following this command Now, open src/main.js file and edit. In this code, we use two parameter mode and route array. If here we do not define any mode by default it takes a hash(#) but in the project, we use a mode History. Next, for better style here we use bootstrap so install bootstrap using this flowing this command. After successfully install bootstrap import bootstrap file in App.vue file. Create three new component inside the component folder. HomeComponent DashboardComponent NewsComponent Now, import all this component in the main.js file because we use to these components to assign the different routes and create an array. Ok, we have registered the all components with routes successfully. Now, add the following code inside App.vue file. this is work as a navigation. After, we start the development server using the following command. Finally, we complete the Vue routing tutorial with mode history tutorial, history mode of routing is working great. Now, when we directly access the route without include hash(#).
const router = new VueRouter({
mode: 'history',
routes: [...]
})
Routing in vuejs with history mode tutorial
Step:1 Install vuejs
npm install -g @vue/cli
# OR
yarn global add @vue/cli
vue create history
Step:2 Install vuejs routing
yarn add vue-router
# or
npm install vue-router --save
// main.js
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
Vue.config.productionTip = false
const routes = [];
const router = new VueRouter({ mode: 'history', routes: routes });
new Vue({
render: h => h(App),
router
}).$mount('#app')
yarn add bootstrap
# or
npm install bootstrap --save
App.vue
...
import 'bootstrap/dist/css/bootstrap.min.css';
...
Step:3 Create Component
// HomeComponent.vue
<template>
<p>This is Home Component</p>
</template>
<script>
export default {
}
</script>
// DashboardComponent.vue
<template>
<p>This is Dashboard Component</p>
</template>
<script>
export default {
}
</script>
// NewsComponent.vue
<template>
<p>This is News Component</p>
</template>
<script>
export default {
}
</script>
Step:4 Import all the components in the main.js file
// main.js
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import HomeComponent from './components/HomeComponent.vue'
import DashboardComponent from './components/DashboardComponent.vue'
import NewsComponent from './components/NewsComponent.vue'
import 'bootstrap/dist/css/bootstrap.min.css';
Vue.config.productionTip = false
const routes = [
{ path: 'home', name: 'Home', component: HomeComponent },
{ path: 'news', name: 'News', component: NewsComponent },
{ path: 'dashboard', name: 'Dashboard', component: DashboardComponent },
];
const router = new VueRouter({ mode: 'history', routes: routes });
new Vue({
render: h => h(App),
router
}).$mount('#app')
<template>
<div id="app">
<nav class="navbar navbar-expand-sm bg-light">
<ul class="navbar-nav">
<li class="nav-item">
<router-link :to="{ name: 'Home' }" class="nav-link">Home</router-link>
</li>
<li class="nav-item">
<router-link :to="{ name: 'Dashboard' }" class="nav-link">Dashboard</router-link>
</li>
<li class="nav-item">
<router-link :to="{ name: 'News' }" class="nav-link">News</router-link>
</li>
</ul>
</nav>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'app'
}
</script>
npm run serve
About the author
Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...
You must be logged in to post a comment.