個人テストは成功しました。誰もが私にさらに指導を与え、あらゆる種類の批判や修正を個人的に受け入れることができます。
フロントエンドのクロスドメインについて心配する必要はありません。シンプルかつ高速で、わずか 4 つのステップで完了します。
ステップ 1: 次のコードを vue.config.js ファイルに追加します。
(vue.config.jsファイルがない場合は新規作成してください。ファイル名が正しいと読み取れません。)
const {
defineConfig } = require("@vue/cli-service");
module.exports = defineConfig({
transpileDependencies: true,
//主要代码******start
devServer: {
**proxy: {
"/api": {
target: "http://116.176.33.120:58081/prod-api/rygs",
ws: true,
changeOrigin: true,
pathRewrite: {
"^/api": ""
},
},
},
//主要代码*********end
},
});
ステップ 2: axios をインストールして構成する
1.axiosをインストールする
npm install axios -g
または
npm i axios -g
2.axiosをグローバルに導入
main.jsに次のコードを導入します。
main.js文件肯定有,不需要新建哦,注意把axios挂载到Vue原型上时前面的 $ 符号。
import axios from ”axios“
Vue.prototype.$axios=axios
import axios from ”axios“
Vue.prototype.$axios=axios
第三段階
リクエストを送信する必要がある場所に次のコードを入力します。
this.$axios
.post("api/输入请求路径", { 请求参数: 请求参数值 })//这里的api/必须加
.then(res => { console.log("res", res))
.catch(err => console.log("err", err));
4番目のステップ
重要なステップ
你成功了!!!