Learn about Vue routers
VueJS is a front-end library that can be used in any back-end language. If you want to create a fully functional front-end application, then vue-router and vue-resource are two great key elements in VueJS.
Today's article will delve into the Vue router. Routing is the way to manage components in Single Page Applications (SPA). All famous front-end frameworks use the concept of Routing.
What is a Vue router?
Vue router is the official router for Vue.js. It integrates deeply with the Vue.js core to help build SPAs with Vue.js easily. Features include:
- Nested route / view mapping
- Configure the router based on components, modules
- Routing parameters, queries, wildcards
- See the transition effects provided by the transition system of Vue.js
- Fine-grained navigation control
- Link to the active CSS class automatically
- HTML5 history mode or hash mode, with auto-fallback in IE9
- Custom scrolling behavior
How to use vue-router
Step 1: Create 3 components inside the Components folder
In the directory, create 3 component files. These files will look like in the following snippet:
// Home.vue
Home
Then create an About.vue file .
// About.vue
About us
Finally, create the Contact.vue file .
// Contact.vue
Contact us
Step 2: Update the index.html file and add the app id attribute.
Step 3: Configure the vue-router module
In the main.js file, we first need to import the vue-router module from the node_modules directory because we have installed all the dependencies in this project. Copy the following code into main.js.
// main.js import Vue from 'vue'; import VueRouter from 'vue-router'; Vue.use(VueRouter); import Home from './components/Home.vue'; import About from './components/About.vue'; import Contact from './components/Contact.vue'; const router = new VueRouter({ mode: 'history', base: __dirname, routes: [ { path: '/', component: Home }, { path: '/about', component: About }, { path: '/contact', component: Contact } ] }); new Vue({ router, template: `
` }).$mount('#app');
Step 4: Run the code and see the final output
Now, there are 3 items in the Navigation bar and if you click on one of them, the route below will change and you can see the vue-router application in action. The example has included all the main project files here as well ((you can also find this project on Github: https://github.com/KrunalLathiya/playground-UjLAnHRe).
package.json
{ "name": "vuerouter", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "webpack-dev-server --host 0.0.0.0 --disable-host-check" }, "author": "", "license": "ISC", "devDependencies": { "babel-core": "^6.25.0", "babel-loader": "^7.1.1", "babel-plugin-transform-runtime": "^6.23.0", "babel-preset-es2015": "^6.24.1", "babel-preset-stage-3": "^6.24.1", "babel-runtime": "^6.25.0", "vue-loader": "^13.0.2", "vue-template-compiler": "^2.4.2", "webpack": "^3.4.1", "webpack-dev-server": "^2.6.1" }, "dependencies": { "vue": "^2.4.2", "vue-router": "^2.7.0" } }
webpack.config.js
var path = require('path'); module.exports = { // This is the "main" file which should include all other modules entry: path.join(__dirname,'/main.js'), // Where should the compiled file go? output: { filename: 'bundle.js' }, resolve: { alias: { vue: 'vue/dist/vue.js' } }, module: { // Special compilation rules loaders: [ { // Ask webpack to check: If this file ends with .js, then apply some transforms test: /.js$/, // Transform it with babel loader: 'babel-loader', // don't transform node_modules folder (which don't need to be compiled) exclude: /node_modules/ }, { // Ask webpack to check: If this file ends with .vue, then apply some transforms test: /.vue$/, // don't transform node_modules folder (which don't need to be compiled) exclude: /(node_modules|bower_components)/, // Transform it with vue use: { loader: 'vue-loader' } } ] }, devServer: { port: 3000 } }
main.js
import Vue from 'vue'; import VueRouter from 'vue-router'; Vue.use(VueRouter); import Home from './components/Home.vue'; import About from './components/About.vue'; import Contact from './components/Contact.vue'; const router = new VueRouter({ mode: 'history', base: __dirname, routes: [ { path: '/', component: Home }, { path: '/about', component: About }, { path: '/contact', component: Contact } ] }); new Vue({ router, template: `
` }).$mount('#app');
index.html
Home.vue
Home
About.vue
About us
Contact.vue
Contact us
You should read it
- What is Travel router?
- 3 ways hackers can attack home routers
- What is a dual-band and tri-band router?
- The router is not as safe as you think
- 11 best long-range Wifi routers 2018
- Good hackers find and patch the vulnerability for more than 100,000 other routers
- About Modem and Router
- Three critical holes in Linksys routers, hackers can take advantage of hijacking
May be interested
- Extremely powerful Wi-fi transmitter for businesseswhile offices or small businesses may not need a network that runs too fast or up to standard, larger offices and businesses need special routers to handle high traffic and security needs.
- Special features of ASUS WiFi transmitter that not everyone knowswhen talking about the special features of wifi routers, asus routers cannot be left out. asus has created a series of outstanding router products with impressive features.
- What is a dual-band and tri-band router?many modern wireless routers have used dual-band, and now router manufacturers are launching tri-band routers. but do they really speed up your wi-fi?
- Appears new malware HiatusRAT targeting enterprise routersa new malware campaign, called hiatus, is targeting small business routers to steal data and track victims.
- 8 steps to increase security for wireless routersmost it equipment today has plug-and-play capabilities, but there is one device that you should never use this way: a wireless router.
- Notable indoor network devices at IFA 2018ifa 2018 brings many of the most modern in-house network products today, from low-latency gaming routers to signaling, amplification, signal boosting, hybrid network coverage has a smart virtual assistant. here are some notable names.
- 7 ways to take advantage of old wireless routersin the case of switching internet service providers, you will be asked to return the old device. but if you have a backup router in your home, here are some ways you can reuse it.
- How to avoid traffic congestion in the home networkin this tutorial we will show you how to configure wi-fi routers to optimize data transfer and performance.
- Dangerous worms target routers and modemsa new worm has been discovered that can inject up to 55 dsl / cable modems and home routers, including names still used by many people.
- Three critical holes in Linksys routers, hackers can take advantage of hijackinglinksys e series routers can get three vulnerabilities that help hackers gain control.