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

vue路由传参与axios

作者: 习惯h | 来源:发表于2018-09-26 20:20 被阅读0次
    1.路由传参

    1.查询字符串:
    /user/regist?uname=jack&upwd=123
    接收:
    {{$route.query}}

    2.rest风格传参
    /user/login/rose/456

    <div id='app'>
       <router-link to='/index'>首页</router-link>
       <router-link to='/user'>用户页</router-link>
       <router-view></router-view>
      </div>
       <script src="Vue/dist/vue.js"></script>
        <script src="Vue-router/dist/vue-router.js"></script>
       <script>
           //2.创建组件
           var  Index={
               template:`
                 <h1>这是首页</h1>
               `
           }
           var User={
               template:`
               <div>
                 <h1>这是用户页</h1>
                 <ul>
                   <li>
                      <router-link to='/user/regist?uname=jack&upwd=123456'>注册</router-link>
                   </li> 
                    <li>
                      <router-link to='/user/login/rose/123456'>登录</router-link>
                   </li>
                </ul>
                <router-view></router-view>
               </div>
             `
           }
           
           
           var Regist={
               template:`
                 <h3>这是注册页</h3>
                 <a href="#">{{$route.query}}</a>
                 <a href="#">
                     uname:{{$route.query.uname}}
                 </a>
                 <a href="#">
                     upwd:{{$route.query.upwd}}
                 </a>
              `
           }
           
            var Login={
               template:`
                <div>
                 <h3>这是登录页</h3>
                 <a>{{$route.params}}</a>
                 <a>{{$route.params.uname}}</a>
                 <a>{{$route.params.upwd}}</a>
                </div>
              `
           }
           
           //3.配置路由
           const routes=[
               {path:'/',component:Index},
               {path:'/index',component:Index},
               {
                   path:'/user',
                   component:User,
                   children:[
                       {path:'regist',component:Regist},
                       {path:'login/:uname/:upwd',component:Login}
                   ]
               }
           ]
           
           //4.创建路由实例
           const router=new VueRouter({
               routes:routes
           })
           
           //5.把路由实例挂载到vue实例上
          new Vue({
              el:'#app',
              router:router//注册路由
          })      
     </script>
    

    2.axios

    版本:
    1.0:vue-resource
    2.0:axios 库
    作用:前段页面和后台数据做交互
    安装http-server: npm install http-server -g
    下载axios: npm install axios
    开启服务: http-server

    <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"></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:'first.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>
    

    相关文章

      网友评论

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

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