Here, we will learn about how to create master page concept in vue.js. Master page concept is also known as theme setup. Its like setting up the pages for layout like sidebar, header, etc. which are common in entire application.
Master page concept:
Step 1: Install the following packages in vue.js:
npm install vue-router
Step 2: Open Home.vue and add the following in it:
<template> <div class="home"> <h1>{{ msg }}</h1> </div> </template> <script> export default { name: 'Home', data () { return { msg: 'Welcome to Home Page' } } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> h1, h2 { font-weight: normal; } </style>
Step 3: Open About.vue and add the following in it:
<template> <div id="about"> When you have a great story about how your product or service was built to change lives, share it. The "About Us" page is a great place for it to live, too. Good stories humanize your brand, providing context and meaning for your product. What’s more, good stories are sticky -- which means people are more likely to connect with them and pass them on. </div> </template> <script> export default { name: 'about' } </script> <!-- styling for the component --> <style> #about { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } </style>
Step 4: Open App.vue and add the following in it:
<template> <div id="app"> <img src="./assets/logo.png" /> <div class="routerlink"> <router-link v-bind:to="'/'">Home</router-link> <router-link v-bind:to="'/about'">About</router-link> </div> <router-view /> </div> </template> <script> export default { name: 'App' } </script> <style> #app { font-family: "Avenir", Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } .routerlink { text-align: center; padding: 1rem; } </style>
Step 5: Open index.js and add the following in it:
import Vue from 'vue' import Router from 'vue-router' import HelloWorld from '@/components/HelloWorld' import About from '@/components/About' Vue.use(Router) export default new Router({ mode: 'history', routes: [ { path: '/', name: 'Home', component: Home }, { path: '/about', name: 'About', component: About } ] })
That’s it. Now we are ready to use the master pages with routes in our application.