axios

axios

vue的 ajax(前端页面和后台数据做交互)。
\color{red}{html部分}

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src='js/vue.js'></script>
<script src='js/vue-router.js'></script>
<script src='js/axios.js'></script>
<style>
    *{
        text-align: center;
    }
    .active{
        color:red;
    }
</style>
</head>
<body>
<div id="app">
   <router-link to='/home'>首页</router-link>
   <router-link to='/user'>用户页</router-link>
   <router-view></router-view>
</div>
<script>
    var Home={
        template:`
            <h1>俺是首页</h1>
    `
    }
    var User={
        template:`
            <div>
                <h1>俺是用户页</h1>
                <table border=1 cellspacing=0 text-center>
                   <thead style="background:red;">
                       <tr>
                         <th>编号</th>
                         <th>品名</th>
                         <th>单价</th>
                         <th>数量</th>
                         <th>小计</th>
                       </tr>
                   </thead>
                   <tbody style="background:red;">
                      <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:'2.json'
            }).then(function(suc){
                console.log(suc.data);
                self.list=suc.data
            }).catch(function(wro){
                console.log(wro)
            })
        }
    }
    const xxx=[
        {path:'/home',component:Home},
        {path:'/user',component:User}
    ]
    const www=new VueRouter({
        routes:xxx,
        linkActiveClass:'active'
    })
    new Vue({
        el:'#app',
        router:www
    })
</script>
</body>
</html>

\color{red}{json部分}
\color{orange}{json部分代码较为严格}

[
{
    "num":1,
    "pname":"苹果",
    "price":3,
    "count":2,
    "sub":6
},
{
    "num":2,
    "pname":"梨子",
    "price":4,
    "count":4,
    "sub":16
},
{
    "num":3,
    "pname":"香蕉",
    "price":6,
    "count":3,
    "sub":18
}
]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容