当前位置: 首页> 汽车> 行情 > 中国软件开发公司排行_无锡企业网站公司_南宁优化网站收费_微信推广怎么弄

中国软件开发公司排行_无锡企业网站公司_南宁优化网站收费_微信推广怎么弄

时间:2025/7/9 6:16:47来源:https://blog.csdn.net/qq_61715584/article/details/142760016 浏览次数: 0次
中国软件开发公司排行_无锡企业网站公司_南宁优化网站收费_微信推广怎么弄

文章目录

    • Axios 网络请求
      • 1.Axios 使用
        • 1.Axios 简介
        • 2.Axios 安装
          • 安装命令
        • 3.Axios 引入方式
          • 全局引入
          • 局部引入
      • 2.整合 vue
          • 1.在组件中使用 axios 发送请求
          • 发送结果
          • 这里就出现了跨域问题
      • 3.跨域
          • 后端解决办法
            • 全局配置类
          • 加入注解 @CrossOrigin
          • 请求结果
        • 全局配置 baseUrl

Axios 网络请求

1.Axios 使用

1.Axios 简介

Axios是封装的Ajax的一个框架
在这里插入图片描述

2.Axios 安装

这是官方网站
在这里插入图片描述

安装命令

三选一就好,看自己使用的什么包管理器

npm install axiosbower install axiosyarn add axios

在这里插入图片描述

https://www.axios-http.cn/docs/intro
在这里插入图片描述

3.Axios 引入方式
全局引入

main.js

// src/main.js  
import { createApp } from 'vue';  
import App from './App.vue';  
import axios from 'axios'; // 导入你配置的Axios实例  const app = createApp(App);  // 将axios注册为全局属性  
app.config.globalProperties.$http = axios
axios.defaults.baseURL="http://localhost:8088"app.mount('#app');
局部引入
import axios from 'axios';

2.整合 vue

请求方式:
在这里插入图片描述

1.在组件中使用 axios 发送请求

在这里插入图片描述

<template><div><h1>{{ title }}</h1></div>
</template><script>
import axios from 'axios';
import { onMounted } from 'vue';
export default {name:"Movie",props:["title"],data:function name(params) {return {}},created:function(){console.log("movie is creating")axios.get("http://localhost:8088/user").then(function(res){console.log(res)})}
}</script>
  created:function(){console.log("movie is creating")axios.get("http://localhost:8088/user").then(function(res){console.log(res)})

这里我选择的是在 movie 组件被创建的时候发送网络请求

发送结果

在这里插入图片描述
并且打开我的后端,确认了 localhost :8088/user 这个接口能够获得数据
在这里插入图片描述
在这里插入图片描述

这里就出现了跨域问题

3.跨域

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

后端解决办法
全局配置类

在这里插入图片描述

加入注解 @CrossOrigin
@CrossOrigin

在这里插入图片描述
只需要在需要跨域的控制器上加入 @CrossOrigin

请求结果

在这里插入图片描述
在这里插入图片描述
已经成功拿到了数据

全局配置 baseUrl

import axios from 'axios'axios.defaults.baseURL="http://localhost:8088"app.config.globalProperties.$http = axios

加入这三行代码,必须是 vue 3 ,vue2配置语法不一样

这时候,movie组件中的发送请求代码就可以这样写了

 created:function(){console.log("movie is creating")this.$http.get("/user").then(function(res){console.log(res)})}
关键字:中国软件开发公司排行_无锡企业网站公司_南宁优化网站收费_微信推广怎么弄

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: