美文网首页
路由的传参与axios

路由的传参与axios

作者: 好久不见_3217 | 来源:发表于2018-09-26 19:24 被阅读0次

    1.路由的传参

    1.查询字符串

    /user/regist?uname=jack&upwd=123"
    

    接收

    {{$route.query}}
    

    2.rest风格传参

    /user/login/rose/456   栽培置路由中{path"login/:uname/:upwd"}
    

    接收

    {{$route.params}}
    

    例:

    var Home={
            template:`
            <div>这是首页</div>
            `
        }
        var User={
            template:`
            <div>
             这是用户页
             <p>
             <router-link to="/user/regist?uname=jack&upwd=123">注册</router-link>
             </p>
             <p>
             <router-link to="/user/login/rose/456">登录</router-link>
             </p>
             <router-view></router-view>
            </div>
            `
        }
        //查询字符串
        var Regist={
            template:`
            <div>
             <h1>这是注册页</h1>
             <a href="">{{$route.query}}</a>
             <a href="">uname:{{$route.query.uname}}</a>
             <a href="">upwd:{{$route.query.upwd}}</a>
            </div>
    
            `
        }
        //rest风格传参
        var Login={
            template:`
            <div>
              <h1>这是登录页</h1>
             <a href="">{{$route.params}}</a>
             <a href="">{{$route.params.uname}}</a>
             <a href="">{{$route.params.upwd}}</a>
            </div>
    
            `
        }
        const routes=[
            {path:"/",component:Home},
            {path:"/home",component:Home},
            {path:"/user",
                component:User,
                children:[
                    {path:"regist",component:Regist},
                    {path:"login/:uname/:upwd",component:Login}
                ]
            }
        ]
        const router=new VueRouter({
            routes:routes,
        })
        new Vue({
            el:"#app",
            router:router
        })
    

    效果如图:


    Image 1.png Image 3.png

    2.axios

    1.0 vue-resource
    2.0 axios库
    axios是vue中的ajax
    ajax:前端页面和后台数据做交互
    建json文件
    发送数据的两种方式:get post

    http-server

    安装命令:
    npm install http-server -g
    开始一个服务:
    http-server
    

    例:

    json文件中的内容

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

    JS部分

    <script src="js/vue.js"></script>
    <script src="js/vue-router.js"></script>
    <script src="js/axios.js"></script>
    <script>
        var Home={
            template:`
            <h1>这是首页</h1>
        `
        }
        var User={
            template:`
            <div>
            <h1>这是用户页</h1>
            <table border="1" cellspacing="0">
                     <thead>
                         <tr>
                           <th>编号</th>
                           <th>名称</th>
                           <th>单价</th>
                           <th>数量</th>
                           <th>小计</th>
                         </tr>
                     </thead>
                    <tbody>
                     <tr v-for="value in list">
                        <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{
                    list:null
                }
            },
            mounted:function(){
                var self=this;
                axios({
                    method:"get",
                    url:'fruit.json'
                }).then(function(resp){
                    console.log(resp.data)
                    self.list=resp.data;
                }).catch(function(err){
                    console.log(err)
                })
            }
        }
        const routes=[
            {path:'/',component:Home},
            {path:'/home',component:Home},
            {path:'/user',component:User}
        ]
        const router=new VueRouter({
            routes:routes,
            linkActiveClass:'active'
        })
    
        new Vue({
            el:'#app',
            router:router
    
        })
    </script>
    

    效果如图:


    Image 4.png

    相关文章

      网友评论

          本文标题:路由的传参与axios

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