路由 路由的傳參 路由的嵌套 axios

路由:vue-router 帶三方工具庫
創(chuàng)建單頁面應用 spa (single page application)
通過不同的URL訪問不同的頁面
下載:
npm install vue
npm install vue-router

1.制作一個簡單的路由

 <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Document</title>
   <style>
/*
        .router-link-active{
            text-decoration: none;
            background: orange;
            color:black;
        }
*/
    .active{
        text-decoration: none;
        background: orange;
        color:black;
    }
  </style>
</head>
<body>
 <div class="itany">
<!--      1.-->
   <router-link to='/index'>首頁</router-link>
   <router-link to='/about'>詳情頁</router-link>
<!--       盛放導航內(nèi)容-->
<router-view></router-view>
 </div>
<script src="vue.js"></script>
<script src="vue-router.js"></script>
<script>
//        2.創(chuàng)建組件
    var Index={
        template:`
        <h1>我是首頁</h1>
    `
    }
    var About={
        template:`
        <h1>我是詳情頁</h1>    
    `
    }
//        3.配置路由
    const routes=[
        {path:'/',component:Index},
        {path:'/index',component:Index},
        {path:'/about',component:About}
    ]
//        4.創(chuàng)建一個路由實例
    const router=new VueRouter({
        routes:routes,
        linkActiveClass:'active'
        
    })
//        5.把路由實例掛載到vue實例上
    new Vue({
        el:'.itany',
        router:router
    })
    </script>
</body>
</html>


注釋:點擊之后可跳轉(zhuǎn),并出現(xiàn)自己所設置的樣式
2.路由的傳參:
查詢字符串:
/user/regist?uname=jack&&upwd=123
接收:{{
route.query}} rest風格傳參 /user/login/rose/456 接收: {{
route.params}}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>路由的傳參</title>
</head>
<body>
   <div id="app">
       <router-link to="/index">首頁</router-link>
       <router-link to="/about">用戶頁</router-link>
       <router-view></router-view>
 </div>
<script src="vue.js"></script>
<script src="vue-router.js"></script>
<script>
//        2.創(chuàng)建組件
    var Index={
        template:`
        <h1>這是首頁</h1>
    `
    }
    var About={
        template:`
            <div>
                <h1>這是用戶頁</h1>
                <ul>
                    <li>
                        <router-link to="/about/user?uname=jack&upwd=123">注冊</router-link>
                    </li>
                    <li>
                        <router-link to='/about/login/rose/456'>登錄</router-link>
                    </li>
                </ul>
                <router-view></router-view>
            </div>
        `
    }
    var User={
        template:`
        <div>
            <h3>我是注冊頁</h3>
            <a href="#">{{$route.query}}</a>
            <ul>
            <li>{{$route.query.uname}}</li>
            <li>{{$route.query.upwd}}</li>
            </ul>
        </div>
        `
    }
    var Login={
        template:`
        <div>
            <h3>我是登錄頁</h3>
            <a href="#">{{$route.params}}</a>
            <ul>
                <li>{{$route.params.userName}}</li>
                <li>{{$route.params.password}}</li>
            </ul>
        </div>
        `
    }
//        3、配置路由
    const routes=[
       {path:'/',component:Index}, {path:'/index',component:Index},
        {
            path:'/about',
            component:About,
            
        children:[
            {path:'user',component:User},
            {path:'login/:userName/:password',component:Login},
        ]
        },
    ]
//        4.創(chuàng)建路由實例
    const router=new VueRouter({
        routes:routes
    })
//        5.
    new Vue({
        el:'#app',
        router:router//注冊路由
    })
    </script>
</body>
</html>

點擊前



點擊后


3.路由的嵌套

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>路由的嵌套</title>
</head>
<body>
   <div id="app">
       <router-link to="/index">首頁</router-link>
       <router-link to="/about">用戶頁</router-link>
       <router-view></router-view>
   </div>
<script src="vue.js"></script>
<script src="vue-router.js"></script>
<script>
//        2.創(chuàng)建組件
    var Index={
        template:`
        <h1>這是首頁</h1>
    `
    }
    var About={
        template:`
            <div>
                <h1>這是用戶頁</h1>
                <ul>
                    <li>
                <router-link to="/about/user">注冊</router-link>
                    </li>
                    <li>
                <router-link to="/about/login">登錄</router-link>
                </li>
                </ul>
                <router-view></router-view>
            </div>
        `
    }
    var User={
        template:`
            <h3>我是注冊頁</h3>
        `
    }
    var Login={
        template:`
            <h3>我是登錄頁</h3>
        `
    }
//        3、配置路由
    const routes=[
       {path:'/',component:Index}, {path:'/index',component:Index},
        {
            path:'/about',
            component:About,
            
        children:[
            {path:'user',component:User},
            {path:'login',component:Login},
        ]
        },
    ]
//        4.創(chuàng)建路由實例
    const router=new VueRouter({
        routes:routes
    })
//        5.
    new Vue({
        el:'#app',
        router:router//注冊路由
    })
    </script>
</body>
</html>

點擊前:



點擊后:


4.axios
vue中的ajax 插件
下載axios:
npm install axios
1.0 vue-resource
2.0 axios
安裝http-server
npm install http-server -g
使用http-server 開啟一個服務器
例:
html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
/*
        .router-link-active{
            text-decoration: none;
            background: orange;
            color:black;
        }
*/
    .active{
        text-decoration: none;
        background: orange;
        color:black;
    }
</style>
</head>
<body>
   <div class="itany">
<!--      1.-->
   <router-link to='/index'>首頁</router-link>
   <router-link to='/about'>詳情頁</router-link>
<!--       盛放導航內(nèi)容-->
  <router-view></router-view>
 </div>
<script src="vue.js"></script>
<script src="vue-router.js"></script>
<script src="axios.js"></script>
<script>
//        2.創(chuàng)建組件
    var Index={
        template:`
        <h1>我是首頁</h1>
    `
    }
    var About={
        template:`
        <div>
          <h1>我是詳情頁</h1>
           <table border=1 cellspacing=0>
            <thead>
                <tr>
                   <td>編號</td>
                   <td>品名</td>
                   <td>單價</td>
                   <td>數(shù)量</td>
                   <td>小計</td>
                </tr>
            </thead>
            <tbody>
               <tr v-for="value in fruList">
                   <td>{{value.num}}</td>
                   <td>{{value.pname}}</td>
                   <td>{{value.price}}</td>
                   <td>{{value.count}}</td>
                   <td>{{value.sub}}</td>
               </tr>
            </tbody>
           </table>
         </div>
    `,
        data:function(){
            return{
                fruList:null
            }
        },
        mounted:function(){
            var self=this;
            axios({
                method:'get',//發(fā)送數(shù)據(jù)的方式
                url:'fruit.json'
            }).then(function(resp){
                console.log(resp.data)
                self.fruList=resp.data
            }).catch(function(err){//請求失敗
                console.log(err)
            })
        }
    }
//        3.配置路由
    const routes=[
        {path:'/',component:Index},
        {path:'/index',component:Index},
        {path:'/about',component:About}
    ]
//        4.創(chuàng)建一個路由實例
    const router=new VueRouter({
        routes:routes,
        linkActiveClass:'active'
        
    })
//        5.把路由實例掛載到vue實例上
    new Vue({
        el:'.itany',
        router:router
    })
    </script>
</body>
</html> 

json:

[
    {
    "num":1,
    "pname":"apple",
    "price":3,
    "count":4,
    "sub":12
},
 {
    "num":2,
    "pname":"pear",
    "price":4,
    "count":5,
    "sub":20
},
 {
    "num":3,
    "pname":"orange",
    "price":5,
    "count":6,
    "sub":30
}
]

進入127.0.0.1:8080



點擊html:



點擊詳情頁:
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 相關(guān)概念 混合開發(fā)和前后端分離 混合開發(fā)(服務器端渲染) 前后端分離后端提供接口,前端開發(fā)界面效果(專注于用戶的交...
    他愛在黑暗中漫游閱讀 3,017評論 4 45
  • 每天早上準時6.30醒來,便匆匆洗漱整理準備出門,吃著昨夜買好的面包,坐上一輛開往市區(qū)的巴士,經(jīng)過一個半小時的搖晃...
    _微葉_閱讀 558評論 0 0
  • 越簡單,越復雜;越簡單,越智慧;越簡單,越普適;越簡單,越便捷。而簡單又不僅僅是簡單,其背后隱藏著改變世界的力量!...
    從未放棄努力閱讀 1,514評論 0 3
  • 把優(yōu)秀變成一種習慣…… 好的 改變自己 1.堅持練習,獲得好身體。 2.精神訓練,痛苦是成長 3.智利培養(yǎng),多讀書...
    LoveYoga喵喵閱讀 211評論 0 0
  • 1964年3月13日夜晚3時20分,在美國紐約郊外某公寓前,一位叫朱諾比白的年輕女子在歸家的途中,遇到了意欲行兇之...
    瀟灑閱讀 372評論 0 1

友情鏈接更多精彩內(nèi)容