美文网首页
路由 路由的传参 路由的嵌套 axios

路由 路由的传参 路由的嵌套 axios

作者: 纪美 | 来源:发表于2018-09-25 16:04 被阅读0次

    路由:vue-router 带三方工具库
    创建单页面应用 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>
    <!--       盛放导航内容-->
    <router-view></router-view>
     </div>
    <script src="vue.js"></script>
    <script src="vue-router.js"></script>
    <script>
    //        2.创建组件
        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.创建一个路由实例
        const router=new VueRouter({
            routes:routes,
            linkActiveClass:'active'
            
        })
    //        5.把路由实例挂载到vue实例上
        new Vue({
            el:'.itany',
            router:router
        })
        </script>
    </body>
    </html>
    

    注释:点击之后可跳转,并出现自己所设置的样式
    2.路由的传参:
    查询字符串:
    /user/regist?uname=jack&&upwd=123
    接收:{{

    点击后


    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.创建组件
        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.创建路由实例
        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>
    <!--       盛放导航内容-->
      <router-view></router-view>
     </div>
    <script src="vue.js"></script>
    <script src="vue-router.js"></script>
    <script src="axios.js"></script>
    <script>
    //        2.创建组件
        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>数量</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',//发送数据的方式
                    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.创建一个路由实例
        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:



    点击详情页:

    相关文章

      网友评论

          本文标题:路由 路由的传参 路由的嵌套 axios

          本文链接:https://www.haomeiwen.com/subject/bddloftx.html