第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 Vue.js 中使用 axios.request(config)

如何在 Vue.js 中使用 axios.request(config)

藍(lán)山帝景 2021-11-25 19:25:53
我想Axios在我的 vue.js 項(xiàng)目中使用,我想發(fā)送HTTP請(qǐng)求。我在 github 上閱讀了Axios socumentation并在網(wǎng)上檢查了很多示例,但我找不到我的答案。我想定義一個(gè)配置文件并從中讀取請(qǐng)求路徑并使用 Axios 調(diào)用它。有很多 API 需要調(diào)用,并且更喜歡將它們保存在單獨(dú)的文件中。我不想使用axios.get或者axios.post更喜歡使用這種風(fēng)格:// in my APIs file export default {  GetAll: {    method: 'get',    url: '/Book/GetAll'  },  GetById: {    method: 'GET',    url: 'Book/GetById/{id}'  },  Add: {    method: 'POST',    url: 'Book/Add'  }}// Axios instantiation import Axios from 'axios'Vue.use({  Axios})const Server = Axios.create({  baseURL: myUrl})export default Server// in my componentimport Server from './server'import Api from './api'export default {  async mounted() {    var list = (await Server.request(Api.GetAll)).data    var book = (await Server.request(Api.GetById)).data  }}在組件中,我可以獲得列表,但我不知道如何調(diào)用GetByIdAPI。
查看完整描述

1 回答

?
滄海一幻覺(jué)

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊

我建議像這樣包裝所有其余的 api 調(diào)用:


class RestApi {

    constructor() {

        this.client = axios.create({

            baseURL: 'base-url-here'

        });

    }


    async get(url, params = {}) {

        return this.client.request({

            type: 'get',

            url: url,

            params: params

        })

    }


    async post(url, data = {}) {

        return this.client.request({

            type: 'post',

            url: url,

            data: data

        })

    }


    async getById(id) {

        return this.get(`Book/GetById/${id}`)

    }

}


const api = new RestApi();

const response = await api.getById(1);


查看完整回答
反對(duì) 回復(fù) 2021-11-25
  • 1 回答
  • 0 關(guān)注
  • 370 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)