vue中的axios

ajax:前端页面和后台数据做交互

把后台数据输出到前台

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>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="dist/vue.js"></script>
<script src="dist/vue-router.js"></script>
<script src="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>数量</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 rsel=this
            axios({
                method:'get',
                url:'fruit.json'
            }).then(function(resp){
                console.log(resp.data)
                rsel.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
    })
    new Vue({
        el:'#app',
        router:router
    })
</script>
</body>
</html>
[
  {
    "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
  }
]
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,025评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 13,082评论 2 59
  • 1.axios(vue中的ajax)
    信不由衷阅读 458评论 0 0
  • 首先配置通用设置 接着 配置拦截器 最后去配置get方法和post方法 最后 挂载到Vue对象上再main.js中...
    ErrorCode233阅读 4,581评论 0 9
  • 说明:除了提供var的变量声明方法,还提供了将变量绑定到所有的任意作用域中(通常是{..}内部)换句话说:let,...
    LingJJ1100的笔记阅读 793评论 0 0