优质广告供应商

广告是为了更好地支持作者创作

路由传参、axios

一、查找字符串
(传)在路径后跟?(接){{$route.query}}
二、rest风格传参
(传)在路径后/ /
<div class="box">
            <router-link to="/home">首页</router-link>
            <router-link to="/bag">用户页</router-link>
            <router-view></router-view>
        </div>
        <script type="text/javascript" src="js/vue.js" ></script>
        <script type="text/javascript" src="js/vue-router.js" ></script>
        <script>
            var home={
                template:`
                    <h1>欢迎来到我的网站</h1>
                `
            }
            var bag={
                template:`
                   <div>
                        <h1>欢迎来到我的用户页</h1>
                        <ul>
                            <li>
                                <router-link to="/bag/zhu?uname=jack&upwd=123">注册</router-link>
                            </li>
                            <li>
                                <router-link to="/bag/deng/jack/899">登录</router-link>
                            </li>
                        </ul>
                        <router-view></router-view>
                    </div> 
                `
            }
            var zhu={
                template:`
                    <div>
                      <h1>欢迎来到我的注册</h1>
                      <p>{{$route.query}}</p>
                      <p>{{$route.query.uname}}</p>
                      <p>{{$route.query.upwd}}</p>
                    </div>
                `
            }
            var deng={
                template:`
                  <div>
                    <h1>欢迎来到我的登录</h1>
                    <p>{{$route.params}}</p>
                    <p>{{$route.params.uname}}</p>
                    <p>{{$route.params.upwd}}</p>
                </div>
                `
            }
            const routes=[
                {path:'/',component:home},
                {path:"/home",component:home},
                {
                    path:"/bag",
                    component:bag,
                    children:[
                        {path:"zhu",component:zhu},
                        {path:"deng/:uname/:upwd",component:deng}
                    ]
                }
            ]
            const router=new VueRouter({
                routes:routes
            })
            new Vue({
                el:".box",
                router:router
            })
        </script>

axios

axios是vue中的ajax 是vue的一个库
下载axios:npm install axios

开启服务器 $http-server
安装htttp-server:npm install http-server -g

``html
<div class="box">
            <router-link to="/home">首页</router-link>
            <router-link to="/bag">用户页</router-link>
            <router-view></router-view>
            
        </div>
``js
        <script type="text/javascript" src="js/vue.js" ></script>
        <script type="text/javascript" src="js/vue-router.js" ></script>
        <script src="js/axios.js"></script>
        <script>
            var home={
                template:`
                    <h1>欢迎来到我的网站</h1>
                `
            }
            var bag={
                template:`
                   <div>
                        <h1>欢迎来到我的用户页</h1>
                            <table>
                                <thead>
                                    <tr>
                                        <th>编号</th>
                                        <th>商品名称</th>
                                        <th>单价</th>
                                        <th>数量</th>
                                        <th>小计</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr v-for="value in arr">
                                        <td>{{value.num}}</td>
                                        <td>{{value.name}}</td>
                                        <td>{{value.price}}</td>
                                        <td>{{value.shu}}</td>
                                        <td>{{value.sub}}</td>
                                    </tr>
                                </tbody>
                            </table>
                    </div> 
                `,
                data:function(){
                    return{
                        arr:null
                    }
                },
                mounted:function(){
                    var self=this;
                    axios({
                         method:"get",       //get  或    post
                         url:'axios.json' 
                    }).then(function(resp){   //请求成功
                        self.arr=resp.data;
                    }).catch(function(err){  //请求失败
                        console.log(err)
                    })
                }
            }
            const routes=[
                {path:'/',component:home},
                {path:"/home",component:home},
                {path:"/bag",component:bag}
            ]
            const router=new VueRouter({
                routes:routes
            })
            new Vue({
                el:".box",
                router:router
            })
        </script>
``json
[
  {
    "num":1,
    "name":"apple",
    "price":3,
    "shu":2,
    "sub":6
  },
  {
    "num":1,
    "name":"pear",
    "price":3,
    "shu":8,
    "sub":24
  },
 {
    "num":3,
    "name":"orange",
    "price":4,
    "shu":2,
    "sub":8
 }
]

优质广告供应商

广告是为了更好地支持作者创作

推荐阅读更多精彩内容