美文网首页
2018-09-26 路由的传参

2018-09-26 路由的传参

作者: 凯瑟_3108 | 来源:发表于2018-09-26 19:25 被阅读10次

    : 路由的传参

    1.查询字符串:

    /user/regist?uname=jack&upwd=123
    接收:
    {{$route.query}}

    2.rest风格传参

    /user/login/rose/456

    如何使用

    1首先使用git在下载
    输入npm install http-server -g
    2创建虚拟域名
    3使用虚拟域名进行访问

    01 路由嵌套-传参

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style type="text/css">
                .active{color: red;}
                a{text-decoration: none;}
                *{padding: 0;margin: 0;}
                #app{background: yellowgreen;width: 100%;height: 50px;line-height: 50px;text-align: center;font-size: 20px;font-weight: bolder;display: block;}
            </style>
        </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.min.js" type="text/javascript" charset="utf-8"></script>
            <script src="../js/vue-router.js" type="text/javascript" charset="utf-8"></script>
            <script type="text/javascript">
                //2.创建组件
                var Home={
                    template:`
                        <h1>这是首页</h1>
                    `
                }
                
                var User={
                    template:`
                        <div>
                            <h1>这是用户页</h1>
                            <p>
                                <router-link to="/user/register?uname=jack&upwd=123">注册</router-link>
                                <router-link to="/user/logo/rose/2345">登录</router-link>
                            </p>
                            <router-view></router-view>
                        </div>
                    
                    `
                }
                
                var Register={
                    template:`
                        <div>
                            <h1>这是注册页</h1>
                            <a href="">{{$route.query.uname}}</a>
                            <br/>
                            <a href="">{{$route.query.upwd}}</a>
                            <br/>
                            <a href="">{{$route.query}}</a>
                        </div>
                    
                    `
                }
                
                var Logo={
                    template:`
                        <div>
                            <h1>这是登录页</h1>  
                            <a href="">{{$route.params.uname}}</a>
                            <br/>
                            <a href="">{{$route.params.upwd}}</a>
                            <br/>
                            <a href="">{{$route.params}}</a>
                        </div>
                    
                    `
                }
                
                //3.配置路由    
                const  routes=[
                    {path:'/',component:Home},
                    {path:'/home',component:Home},
                    {
                        path:'/user',
                        component:User,
                        children:[
                            {path:'register',component:Register},
                            {path:'logo/:uname/:upwd',component:Logo}
                        ]
                    }
                ]
                
                //4.创建路由实例
                const router=new VueRouter({
                    routes:routes,
                    linkActiveClass:'active'
                })
                
                //5.路由实例挂载vue实例上
                new Vue({el:'#app',router:router})
            </script>
        </body>
    </html>
    

    02 axios.html

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></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" type="text/javascript" charset="utf-8"></script>
            <script src="js/vue-router.js" type="text/javascript" charset="utf-8"></script>
            <script src="js/axios.js" type="text/javascript" charset="utf-8"></script>
            <script type="text/javascript">
                //2.创建组件
                var Home={ template:` <h1>ftjhsrdf</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",//get post
                            url:'fruit.json'
                        }).then(function(resp){//请求成功
                            console.log(resp.data)
                            self.list=resp.data;
                        }).catch(function(err){//请求失败
                            console.log(err)
                        })
                    }
                }
                    
                //3.配置路由    
                const routes=[
                    {path:'/',component:Home},
                    {path:'/home',component:Home},
                    {path:'/user',component:User}
                ]
                //4.创建路由实例
                const router=new VueRouter({
                    routes:routes,
                    linkActiveClass:'active'
                })
                //5.路由实例挂载vue实例上
                 new Vue({
                   el:'#app',
                   router:router
               })
            </script>
        </body>
    </html>
    
    

    fruit.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
        }
    ]
    

    相关文章

      网友评论

          本文标题:2018-09-26 路由的传参

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