3-2 axios基礎介紹

來源:互聯網
上載者:User

標籤:rms   fun   多個   開發   sha   靜態   api   use   pre   

1.靜態引用

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

2.npm方式安裝(推薦)

$ npm install axios --save-save     ==> 是指將包資訊添加到 package.json 裡的 dependencies節點,表示發布時依賴的包。-save-dev ==> 是指將包資訊添加到 package.json 裡的 devDependencies節點,表示開發時依賴的包。

3.八種API (詳細介紹 : https://www.npmjs.com/package/axios

1.axios.request(config)2.axios.get(url[, config])3.axios.delete(url[, config])4.axios.head(url[, config])5.axios.options(url[, config])6.axios.post(url[, data[, config]])7.axios.put(url[, data[, config]])8.axios.patch(url[, data[, config]])

4.接收響應資訊

axios.get(‘/user/12345‘).then(function(response) {    console.log(response.data);         // 響應資料    console.log(response.status);       // 狀態代碼    console.log(response.statusText);   // 伺服器的響應的HTTP狀態資訊    console.log(response.headers);      // 回應標頭    console.log(response.config);       // 提供給`axios`該請求的配置});

5.多個介面一起調用

function getUserAccount() {    return axios.get(‘/user/12345‘);} function getUserPermissions() {    return axios.get(‘/user/12345/permissions‘);} axios.all([getUserAccount(), getUserPermissions()]).then(axios.spread(function (acct, perms) {    // Both requests are now complete}));

 

二.常見的請求

new Vue({    // 全域攔截    mounted: function(){        // 請求前        axios.interceptors.request.use(config => {            console.log("request 請求前");            return config;        })        // 攔截響應的請求        axios.interceptors.response.use(response => {            console.log("request 響應的請求前")            return response;        })    },    methods: {        // get請求        get: function(){            axios.get("package.json", {                params: {                    userId: "999"                },                headers: {                    token: "Alan"                }            }).then(res => {                this.msg = res.data;            }).catch(error => {         // catch是捕獲異常                this.msg = "error" + error;            })        },        // post請求        post: function(){            axios.post("package.json", {                // 參數                userId: "888"            },{                headers: {                    token: "Alanpost"                }            }).then(res => {                this.msg = res.data;            }).catch(error => {         // catch是捕獲異常                this.msg = "error" + error;            })        },        // axios底層配置        http: function(){            axios({                url:"package.json",                method: "get",                // post參數                data: {                    urseId: "101"                },                // get參數                params: {                    userId: "102",                },                headers: {                    token: "http"                }            }).then(res => {                this.msg = res.data;            })        }    }})

 

3-2 axios基礎介紹

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.