axios请求数据
Element UI手冊:cloud.tencent.com/developer/d… 中文文檔:element-cn.eleme.io/#/zh-CN github地址:github.com/ElemeFE/ele…
1:進入項目,npm安裝
npm install axios --save 復制代碼2.在main.js下引用axios
import axios from 'axios' 復制代碼3:準備json數據 自己寫了一個json數據,放在服務器上,現在要通過vue項目調用數據 www.intmote.com/test.json
4:跨域問題,設置代理,利用proxyTable屬性實現跨域請求 在config/index.js 里面找到proxyTable :{} ,然后在里面加入以下代碼
proxyTable: {'/api': {target: 'http://www.intmote.com',//設置你調用的接口域名和端口號 別忘了加httpchangeOrigin: true,//允許跨域pathRewrite: {'^/api': '' //這個是定義要訪問的路徑,名字隨便寫 }} }, 復制代碼5:打開一個界面User.vue,開始寫請求數據的方法
methods: {getData() {axios.get('/api/test.json').then(response => {console.log(response.data);}, response => {console.log("error");});}} 復制代碼 User.vue參考代碼: <template><div id="app"></div> </template> <script>import axios from "axios";export default {name: "app",data() {return {itemList: []}},mounted() {this.getData();},methods: {getData() {axios.get('/api/test.json').then(response => {console.log(response.data);}, response => {console.log("error");});}}} </script>復制代碼6:再次運行 npm run dev
這個時候,我們可以看見,請求的數據
原文作者:祈澈姑娘
轉載于:https://juejin.im/post/5cebad275188252dda0afd45
總結
- 上一篇: 现代IM系统中的消息系统架构 - 模型篇
- 下一篇: 圆方树学习笔记