axios

Vue中的ajax
ajax作用:前端页面和后台数据做交互
在写是需要有一个.json的文档

axios({
  method:'get'//发送请求 get、 post,
  url:'路径'
}).then(function(rext){//请求成功
  console.log(rext.data)
}).catch(function(tex){//请求失败
  console.log(tex)
})

用git来模拟服务器
在执行时,需要使用git安装http-server
安装指令:npm install http-server -g回车等待
开启一个服务:http-server

html、vue

<div id="app">
    <router-link to="/home">首页</router-link>
    <router-link to="/user">登录</router-link>
    <router-view></router-view>
</div>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
<script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.js"></script>
<script src="https://cdn.bootcss.com/axios/0.18.0/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 text">
                         <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{
                text:null
            }
        },
        mounted:function(){
            var thiss=this
            axios({
                method:'get',
                url:'shuju.json'
            }).then(function(aaa){
                thiss.text=aaa.data
            }).catch(function(aaa){
                console.log(aaa)
            })
        }
    }
    const luyou=[
        {path:'/',component:home},
        {path:'/home',component:home},
        {path:'/user',component:user}
    ]
    const router=new VueRouter({
        routes:luyou
    })
    new Vue({
        el:'#app',
        router:router
    })
</script>

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
  }
]
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、创建项目 使用vue开发项目时,通过脚手架工具vue-cli可以很方便的构建项目,热重载、保存时静态检查以及可...
    一恋蝶梦阅读 8,779评论 1 35
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • vue ajax 前端页面和后台数据进行交互 jsonvue 库下载:npm install axio...
    LYH2312阅读 284评论 0 0
  • 下载:npm install axios版本1.0:vue-resource2.0:axios(相当于库)Vue中...
    Cherish丶任阅读 286评论 0 0
  • 和煦春风 吹拂 脸颊两边 尽是桃花朵朵 柳眉弯弯 湖光涟漪 撩起你的秀发 迷离的春色装扮 拐弯处 夜莺欢唱 小瀑布...
    亁乾阅读 355评论 0 0