2018-09-26

axios

axios: vue中的ajax(ajax:前端頁面和后臺數(shù)據(jù)做交互)

axios的應用

 <div id="app"> 
    <router-link to="/home">首頁</router-link> 
   <router-link to="/user">用戶也</router-link> 
  <router-view></router-view> 
    </div>
   <script src="[dist/vue.js](dist/vue.js)"></script> 
   <script src="[dist/vue-router.js](dist/vue-router.js)"></script> 
  <script src="[dist/axios.js](dist/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>數(shù)量</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:'/home',component:Home}, 
        {path:'/user',component:User} 
     ] 
      const router=new VueRouter({ 
         routes:routes 
       }) 
    new Vue({ 
            el:' #app', 
         router:router 
     }) 
    </script> 

它的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
}
]
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容