Vue Learning Notes Advanced article--vue-router installation and use

Source: Internet
Author: User
Tags json
Introduction

Vue-router is Vue.js's official routing plugin, which is deeply integrated with vue.js and is ideal for building single-page applications. Vue's single-page applications are routing-and component-based, and routing is used to set access paths and map paths and components. Traditional page application, is to use some hyperlinks to achieve page switching and jump. In Vue-router single page application, it is the switch between the paths, that is, the switch of the components.
This article is based on the previous article (Vue Learning Notes Advanced article--VUE-CLI installation and introduction) VUE-CLI scaffolding tools. installation

At the terminal, go to the MY-DEMO1 project directory created in the previous article by using the CD command, and then install it using the following command:

   
   
    
    NPM Install Vue-router--save
   
   

The purpose of the--save parameter is to add the corresponding configuration in our package configuration file Package.json file. After the installation is successful, you can view the Package.json file, and you will find more "Vue-router": "^2.7.0" configuration. As follows:

   
   
    
      "Dependencies": {
    
        "vue": "^2.3.3",
    
        "Vue-router": "^2.7.0"
    
      },
   
   
Use

With the above steps, we have installed the Vue-router, but how do we use it in VUE-CLI?
First, we need to import and register the Vue-router in the Main.js file:

   
   
    
    ES6 syntax importing import
    
    vuerouter from ' vue-router '
    
    //Register
    
    vue.use (vuerouter)
   
   

Then it is instantiated:

   
   
    
    Const ROUTER = new Vuerouter ({
    
      mode: ' History ',
    
      routes:[
    
        {path: '/', component:demohome},
    
        {path: '/ About ', component:demoabout},
    
        {path: '/contact ', component:democontact}
    
      ]
    
    })
   
   

Path: is the path to the route.
Component: Is the component that the route needs to render.
Demohome, Demoabout, democontact are all single-file components in the code above, so we also need to create the above three components and import them into the current file. These three components we just use as examples, so it's simpler and the code is as follows: Demohome.vue:

 <template> <div id= "Home" > 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.