路由

2018-09-25  本文已影响0人  执念_6afc

1

路由:vue-router
vue的插件 下载:npm install vue-router
作用:根据不同的url访问不同的页面
创建单页面应用 SPA(single page application)
最简单的路由

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
        <div id='app'>
            <!--      第一步-->
              <router-link to='/home'>首页</router-link>
              <router-link to='/detail'>详情页</router-link>
            <!--       盛放每个页面对应的内容-->
            <router-view></router-view>
         </div>
          <script src='js/vue.js'></script>
          <script src='js/vue-rouetr.js'></script>
          <script>
                //2.创建组件
                 var Home={
                       template:`
                            <h1>这是首页</h1>
                        `
                  }
                  var Detail={
                          template:`
                               <h1>这是详情页</h1>
                          `
                    }
                     //配置路由
                    const routes=[
                      //解决第一次加载时页面没有东西
                    {path:'/',component:Home},
                    {path:'/home',component:Home},
                      {path:'/detail',component:Detail}
                     ]
                  //4.创建一个路由实例
                   const rouetr=new VueRouter({
                        rouets:routes
                   })
                    //把路由挂载到vue实例上
                    new Vue({
                          el:'#app',
                          router:router
                     })
          </script>
</body>
</html>

2路由的传参

1.查询字符串
/user/regist?name=jack&age=18

     接收:{{$route.query}}

2.rest风格
/user/login/jack/18
接收:{{$route.params}}

  <!DOCTYPE html>
  <html lang="en">
  <head>
        <meta charset="UTF-8">
        <title>Document</title>
  </head>
  <body>
        <div id='app'>
              <rouetr-link to='/home'>首页</router-link>
               <router-link to='/user'>用户页</rouetr-link>
               <router-view></router-view>
        </div>
        <script src='js/vue.js'></script>
         <script src='js/vue-router.js'></script>
         <script>
              //2
              var Home={
                  template:`
                       <h1>这是首页</h1>
                  `
              }
              var User={
                  template:`
                      <div>
                          <h1>这是用户页</h1>
                          <ul>
                              <li><router-link to='/user/regist?uname=jack&&age=18'>注册</rouetr-link></li>
                              <li>
                                    <rouetr-link to='/user/login/alice/20'>登录</rouetr-link>
                              </li>
                              <rouetr-view></router-view>
                          </ul>
                      </div>
                  `
              }
              var Regist={
                  template:`
                       <div>
                            <h3>这是注册页面</h3>
                            <a href=''>{{$route.query}}</a>
                            <ul>
                      <li>{{$route.query.uname}}</li>
                            <li>{{$route.query.age}}</li>
                            </ul>
                       </div>
                  `
              } 

              var Login={
                  template:`
                      <div>
                            <h3>这是登录页面</h3>
                            <p>{{$route.params}}</p>
                            <ul>
                                <li>{{$route.params.userName}}</li>
                            <li>{{$route.params.userage}}</li>
                            </ul>
                      </div>
                  `
              }

              const routes=[
                  {path:'/',component:Home},
                  {path:'/home',component:Home},
                  {
                      path:'/user',
                      component:User,
                      children:[
                          {path:'regist',component:Regist},
                                {path:'login/:userName/:userage',component:Login}
                       ]
                   }
              ]
            const router=new VueRouter({
                  routes:routes
            })
            new Vue({
                el:'#app',
                router:router
            })
         </script>
         </body>
  </html>

3路由的嵌套

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
      <div id='app'>
          <router-link to='/home'>首页</router-link>
          <router-link to='/user'>用户页</router-link>
           <router-view></router-view>
      </div>
       <script src='js/vue.js'></script>
       <script src='js/vue-router.js'></script>
       <script>
            var Home={
                template:`<h1>这是首页</h1>`
            }
            var User={
                template:`
                    <div>
                        <h1>这是用户页</h1>
                         <ul>
                              <li>
                                  <router-link to='/user.regist'>注册<router-link>
                              </li>
                              <li>
                                  <router-link to='/user/login'>登录</router-link>
                              </li>
                         </ul>
                        <router-view><router-view>
                    </div>
                `
            }
            var Regist={
                 template:`<h3>这是注册页面</h3>`
            }
            var Login={
                template:`<h3>这是登录页面</h3>`
            }
            const routes=[
                {path:'/',component:Home},
                {path:'/home',component:Home},
                {
                     path:'/user',
                     component:User,
                     children:[
                          {path:'regist',component:Regist},
                      {path:'login',component:Login}
                }
            ]
            const router=new VueRouter({
                  routes:routes
            })
            new Vue({
                el:'#app',
                router:router
            })
        </script>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读