SoFunction
Updated on 2025-04-05

vue-router single page routing

In vue, there is a class library called vue-router, which is used to do single-page routing. Routing is generally divided into four steps:

  • Prepare a root component();
  • You need to prepare the routing content template;
  • Prepare to route new VueRouter();
  • Associated routing map
  • Start the route start(App,'#box'); //The first parameter is the prepared root component, and the second parameter is the location to be bundled is the id defined by yourself.

Download address of vue-router on github:/vuejs/vue-router

The simple code about routing jump is as follows:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <script type="text/javascript" src="js/" ></script>
    <script type="text/javascript" src="js/" ></script>
    <script type="text/javascript" src="js/" ></script>
    <title></title>
  </head>
  <body>
    <div >
      <ul>
        <li>
          <a v-link="{path:'/home'}">I'm the firsta</a>
        </li>
        <li >
          <a v-link="{path:'news'}">I'm the seconda</a>
        </li>
      </ul>
      <div>
        <router-view></router-view>
      </div>
    </div>
  </body>
  <script>
    //1. Prepare a root component    var App=();
    
    // News component preparation    var Home=({
      template:'<h3>I am the first content page of a</h3>'
    });
    
    var News=({
      template:'<h3>I am the second content page of a</h3>'
    })
    
    //3. Prepare the route    var router = new VueRouter();
    
    //4. Related    
    ({
      'home':{
        component:Home
      },
      'news':{
        component:News
      }
    })
    
    //5. Start the routing    
    (App,'#box');
  &lt;/script&gt;
&lt;/html&gt;

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.