日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > vue >内容正文

vue

VueCli 中安装 axios

發(fā)布時間:2023/12/14 vue 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 VueCli 中安装 axios 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

axios 官網(wǎng):axios中文網(wǎng)

方式一:將 axios 綁定到 vue 原型上

安裝:

npm install axios

main.js中導(dǎo)入并綁定

import Vue from 'vue' import App from './App' import axios from 'axios'Vue.prototype.$axios = axiosnew Vue({el: '#app',components: { App },template: '<App/>' })

使用:

<template><div id="app"><button @click="handleClick">點(diǎn)擊</button></div> </template><script> export default {name: "App",methods: {handleClick() {// this.$axios({// method: "get",// url: "http://localhost:8081/hello"// }).then(res => {// console.log(res.data);// });this.$axios.get("http://localhost:8081/hello").then(response => {console.log(response.data);});}} }; </script><style></style>

方式二:使用 vue-axios

安裝:

npm install --save axios vue-axios

main.js 中安裝

import Vue from 'vue' import App from './App'import axios from 'axios' import VueAxios from 'vue-axios' // VueAxios 與 axios 的位置不能交換,否則出現(xiàn) TypeError: Cannot read property 'protocol' of undefined Vue.use( VueAxios , axios)new Vue({el: '#app',components: { App },template: '<App/>' })

使用:

<template><div id="app"><button @click="handleClick">點(diǎn)擊</button></div> </template><script> export default {name: "App",methods: {handleClick() {// this.axios({// method: "get",// url: "http://localhost:8081/hello"// }).then(res => {// console.log(res.data);// });this.axios.get("http://localhost:8081/hello").then(response => {console.log(response.data);});}} }; </script><style></style>

補(bǔ)充:發(fā)送 post 請求

this.axios({method: "post",url: "http://localhost:8081/hello",data: {username: "admin",password: "123"} }).then((res)=>{console.log(res.data); })

后端 springboot 代碼

@RestController @CrossOrigin public class HelloController {@RequestMapping("hello")public String hello(@RequestBody Map<String, String> username) {System.out.println(username);return "Hello world!";} }

結(jié)果:




總結(jié)

以上是生活随笔為你收集整理的VueCli 中安装 axios的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。