解决方案:Springboot+Vue3+Mybatis+Axios 前后端分离项目中 遇见的若干报错和踩坑避坑(一)

简介: Springboot+Vue3+Mybatis+Axios 前后端分离项目中 遇见的若干报错和踩坑避坑


missing script:serve报错

问题:

npm ERR! missing script: serve
npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users.........

问题原因:

这个错误一般是忘记进入项目目录里面而导致的。

解决方案:

只需要先 cd 切换到创建的项目的目录里面再使用npm run

serve,否则就会报错

跨域限制:CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

问题:使用Axios无法成功跨域

Access to XMLHttpRequest at 'http://localhost:8080/api' from origin 'http://localhost:8088' has been blocked by CORS policy: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.

问题原因:proxy代理没配置好,或者springboot端的接口对应不上。

解决方案:1、检查vue.config.js

module.exports = {
  devServer: { // 配置跨域代理
    host: 'localhost',
    port: '8088', //vue程序端口换成8088,避免与Spring Boot项目端口冲突
    https: false,
    open: true,
    proxy: {
      '/api': { // 匹配所有以 '/api'开头的请求路径
        target: 'http://localhost:8080', // 代理目标的基础路径
        changeOrigin: true, // 支持跨域
        pathRewrite: { // 重写路径: 去掉路径中开头的'/api'
          '^/api': ''
        }
      }
    }
  }
}

2、检查axios请求:

axios.get('http://localhost:8080/api/connect').then(function(response) {
            if (response.data) {
              console.log(response.data)
            }
          }).catch(function(error){
            console.log(error);
          })
          console.log("response done!")

3、检查后台代码,在后台controller加上@CrossOrigin注解

@RestController
@CrossOrigin
public class VueTest {
    @GetMapping("/api/connect")
    public String hivue(){
        System.out.println("vue connect success!");
        //System.out.println(mail);
        //System.out.println(password);
        return "regist successs";
    }
}

this.axios is not a function

问题:this.axios is not a function或者this.$axios is not a function

解决方案:Vue抛弃了this的概念,不要再用this.

运行项目失败 显示:npm ERR!  code ELIFECYCLE  errno 1  ..@0.1.0 serve: `vue-cli-service serve`

问题:

code ELIFECYCLE
npm ERR! errno 1
npm ERR! usermana@0.1.0 serve: `vue-cli-service serve`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the usermana@0.1.0 serve script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

问题原因:可能是在某次关闭项目时出错,是node_modules丢失了部分文件

解决方案:删除node_modules,重新配置和安装依赖包

首先删除node_modules,然后强制清除缓存,然后重新安装

rm package-lock.json
npm cache clear --force
npm install
相关文章
|
18天前
|
SQL XML Java
解决Spring Boot项目中的数据库迁移问题
解决Spring Boot项目中的数据库迁移问题
|
19天前
|
负载均衡 Java 开发者
如何在Spring Boot项目中实现微服务架构?
如何在Spring Boot项目中实现微服务架构?
|
19天前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的软件项目管理系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的软件项目管理系统附带文章源码部署视频讲解等
12 0
|
19天前
|
SQL XML Java
解决Spring Boot项目中的数据库迁移问题
解决Spring Boot项目中的数据库迁移问题
|
21天前
|
JavaScript Java 关系型数据库
青戈大佬部署SpringBoot+Vue项目资料,vue中配置文件 .env.development,在Vue目录下 Find in Files
青戈大佬部署SpringBoot+Vue项目资料,vue中配置文件 .env.development,在Vue目录下 Find in Files
|
2月前
|
算法 Java 数据库连接
Spring+MySQL+数据结构+集合,Alibaba珍藏版mybatis手写文档
Spring+MySQL+数据结构+集合,Alibaba珍藏版mybatis手写文档
|
2月前
|
Java 数据库连接 Spring
Spring 整合mybatis
Spring 整合mybatis
32 2
|
1月前
|
Java 数据库连接 mybatis
在Spring Boot应用中集成MyBatis与MyBatis-Plus
在Spring Boot应用中集成MyBatis与MyBatis-Plus
73 5
|
1月前
|
Java 数据库连接 数据库
实现Spring Boot与MyBatis结合进行数据库历史数据的定时迁移
实现Spring Boot与MyBatis结合进行数据库历史数据的定时迁移
42 2
|
1月前
|
缓存 NoSQL Java
在 SSM 架构(Spring + SpringMVC + MyBatis)中,可以通过 Spring 的注解式缓存来实现 Redis 缓存功能
【6月更文挑战第18天】在SSM(Spring+SpringMVC+MyBatis)中集成Redis缓存,涉及以下步骤:添加Spring Boot的`spring-boot-starter-data-redis`依赖;配置Redis连接池(如JedisPoolConfig)和连接工厂;在Service层使用`@Cacheable`注解标记缓存方法,指定缓存名和键生成策略;最后,在主配置类启用缓存注解。通过这些步骤,可以利用Spring的注解实现Redis缓存。
55 2