结合 Vue CLI 和 Spring Boot 创建一个全栈应用,涉及到前端和后端的协同工作。以下是一个简单的示例,展示如何将 Vue CLI 生成的前端应用与 Spring Boot 后端服务结合起来。

步骤 1: 创建 Vue CLI 前端应用

首先,使用 Vue CLI 创建一个新的前端项目:

vue create my-vue-app
  • 1.

按照提示完成项目设置。

步骤 2: 开发前端应用

my-vue-app 目录中开发你的前端应用。例如,创建一个组件来显示从 Spring Boot 后端获取的数据。

<!-- my-vue-app/src/components/MyData.vue -->
<template>
  <div>
    <h1>Data from Backend</h1>
    <p>{{ data }}</p>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      data: null,
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      axios.get('http://localhost:8080/api/data')
        .then(response => {
          this.data = response.data;
        })
        .catch(error => {
          console.error('Error fetching data:', error);
        });
    }
  }
};
</script>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
步骤 3: 创建 Spring Boot 后端应用

在另一个目录中创建 Spring Boot 应用。

  1. 创建 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <!-- 其他配置略... -->
  <dependencies>
    <!-- Spring Boot Starter Web -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Spring Boot Starter Tomcat (默认包含,可以省略) -->
    <!-- 其他依赖略... -->
  </dependencies>
</project>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  1. 创建主应用程序类
// MyApplication.java
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  1. 创建一个控制器来返回数据
// MyController.java
package com.example;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @GetMapping("/api/data")
    public String getData() {
        return "Hello from Spring Boot!";
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
步骤 4: 配置 CORS(如果需要)

根据之前的回答,配置 Spring Boot 应用以解决跨域问题。

步骤 5: 运行后端服务

运行 Spring Boot 应用:

mvn spring-boot:run
  • 1.
步骤 6: 运行前端服务

my-vue-app 目录中运行 Vue CLI 开发服务器:

cd my-vue-app
npm run serve
  • 1.
  • 2.
步骤 7: 访问前端应用

打开浏览器,访问 Vue CLI 开发服务器提供的地址(通常是 http://localhost:8080),你应该能看到前端应用显示从 Spring Boot 后端获取的数据。

这个例子展示了如何将 Vue CLI 和 Spring Boot 结合起来创建一个简单的全栈应用。前端应用使用 Vue CLI 生成,后端服务使用 Spring Boot 创建,并通过 AJAX 请求与后端进行通信。在实际开发中,你可能还需要考虑更多的细节,如环境配置、数据库集成、安全性等。