温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot整合vue项目(小试牛刀)

发布时间:2020-09-20 17:28:20 来源:脚本之家 阅读:138 作者:codecraft 栏目:编程语言


本文主要研究一下如何在springboot工程整合vue

maven

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

新建springboot的web工程,默认会在resources目录下生成static以及templates文件夹

templates文件用于存放后端渲染的模板,这里我们采用前后端分离的方式,因而该文件夹就没有用了

static文件夹就是存放静态文件的地方

plugin

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <!-- mvn process-resources -->
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <executions>
          <execution>
            <id>copy Vue.js frontend content</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>src/main/resources/static</outputDirectory>
              <overwrite>true</overwrite>
              <resources>
                <resource>
                  <directory>${basedir}/vue-demo/dist</directory>
                  <includes>
                    <include>static/</include>
                    <include>index.html</include>
                  </includes>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

这里我们使用了maven-resources-plugin插件,将vue工程npm run build之后的dist文件夹下的文件拷贝到static目录下

这里我们假设vue工程名为vue-demo,在这个springboot工程的根目录下

对于vue工程,首先执行npm run build生成静态文件,之后对maven工程执行mvn process-resources,就可以一键拷贝

小结

在springboot工程整合vue的话,将静态文件拷贝到src/main/resources/static目录下即可,这样就可以在springboot工程打开静态文件了,对api的请求也无需再转发,也没有跨域问题,比较适合管理后台前端资源的整合。

doc

A Lovely Spring View: Spring Boot & Vue.js

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI