博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vue.js 配置axios 用来ajax请求数据
阅读量:5304 次
发布时间:2019-06-14

本文共 4334 字,大约阅读时间需要 14 分钟。

* 用npm 安装 axios

   切换到项目的根目录

npm install --save axios vue-axios

  

* 在vue的入口文件./src/main.js 中引入axios, 添加2行代码

// axiosimport axios from 'axios'Vue.prototype.$http = axios

  

*  ./src/main.js 入口文件如下:

// The Vue build version to load with the `import` command// (runtime-only or standalone) has been set in webpack.base.conf with an alias.import Vue from 'vue'import App from './App'import router from './router'// axiosimport axios from 'axios'Vue.prototype.$http = axios Vue.config.productionTip = false /* eslint-disable no-new */new Vue({  el: '#app',  router,  components: { App },  template: '
'})

* 生成数据文件./assets/data/people.json

{"people":[{"id":1,"name":"Jack","age":30,"gender":"Male"},{"id":2,"name":"Bill","age":26,"gender":"Male"},{"id":3,"name":"Tracy","age":22,"gender":"Female"},{"id":4,"name":"Chris","age":36,"gender":"Male"}]}

  为什么不能直接在组件中访问 ../assets/data/people.json? 

* php生成数据 (需要跨域CORS)

people.json */header('Content-Type:text/json; charset=utf-8'); header('Access-Control-Allow-Origin:*');header('Access-Control-Allow-Methods:GET');header('Access-Control-Allow-Headers:x-requested-with,content-type'); $data = new \StdClass();$data->people = [ [ "id" => 1, "name" => "Jack", "age" => 30, "gender" => "Male" ], [ "id" => 2, "name" => "Bill", "age" => 26, "gender" => "Male" ], [ "id" => 3, "name" => "Tracy", "age" => 22, "gender" => "Female" ], [ "id" => 4, "name" => "Chris", "age" => 36, "gender" => "Male" ], [ "id" => 5, "name" => "guanmengying", "age" => 29, "gender" => "Female" ]];echo json_encode($data, true);

 * 启动php接口

php -S 0.0.0.0:8081

  

* 测试这个接口 可以用浏览器访问http://localhost:8081/person.php

  或者 命令行 curl http://localhost:8081/people.php

 输出:

{"people":[{"id":1,"name":"Jack","age":30,"gender":"Male"},{"id":2,"name":"Bill","age":26,"gender":"Male"},{"id":3,"name":"Tracy","age":22,"gender":"Female"},{"id":4,"name":"Chris","age":36,"gender":"Male"},{"id":5,"name":"guanmengying","age":29,"gender":"Female"}]}

* 路由文件 ./src/router/index.js

 根据默认的HelloWorld照葫芦画瓢即可

import Vue from 'vue'import Router from 'vue-router'import HelloWorld from '@/components/HelloWorld'import person from '@/components/person' Vue.use(Router) export default new Router({  routes: [    {      path: '/',      name: 'HelloWorld',      component: HelloWorld    },    {      path: '/person',      name: 'person',      component: person    }  ]})

* 模板文件 ./src/components/person.vue

  主要的变更:

created: function() {    var v = this;    // vue instance    v.$http      .get("http://localhost:8081/people.php")  // xhr      .then(function(resp) {        v.people = resp.data.people;  // bind data      })      .catch(function(error) {        document.write(error.toString());      });  },  data() {    return {      // ...      people: []  // empty array. null can cause template compiling error    };  },

  * 浏览器访问

csdn博客中插入 emoji 会导致后面的内容失效,要重新编辑。

 所以不要用emoji

CSDN 的这个文章丢失了, 转存到这里。

而且在csdn创建的所有标签为vue的文章也都没有了。

转载于:https://www.cnblogs.com/mingzhanghui/p/9461927.html

你可能感兴趣的文章
脚本删除文件下的文件
查看>>
实用拜占庭容错算法PBFT
查看>>
java b组 小计算器,简单计算器..
查看>>
java的二叉树树一层层输出,Java构造二叉树、树形结构先序遍历、中序遍历、后序遍历...
查看>>
php libevent 定时器,PHP 使用pcntl和libevent实现Timer功能
查看>>
php仿阿里巴巴,php实现的仿阿里巴巴实现同类产品翻页
查看>>
Node 中异常收集与监控
查看>>
七丶Python字典
查看>>
Excel-基本操作
查看>>
面对问题,如何去分析?(分析套路)
查看>>
Excel-逻辑函数
查看>>
面对问题,如何去分析?(日报问题)
查看>>
数据分析-业务知识
查看>>
nodejs vs python
查看>>
poj-1410 Intersection
查看>>
Java多线程基础(一)
查看>>
TCP粘包拆包问题
查看>>
Java中Runnable和Thread的区别
查看>>
SQL Server中利用正则表达式替换字符串
查看>>
POJ 1015 Jury Compromise(双塔dp)
查看>>