前端工具&概念
前端三大框架
:
vue-cli
: Vue.js command line interface
ESLint
: 识别ECMAScript 并且按照规则给出报告的代码检测工具,模块化,静态代码检查(The standard of scripting languages like JavaScript
is ECMAScript
)
WebPack
: 前端的模块化和打包工具,把需要的资源打成一个bundle.js
新建工程
参考教程: https://blog.codecentric.de/en/2018/04/spring-boot-vuejs/
首先保证你已经安装了node
如果是使用brew
可以just do brew install node
1 | sudo npm install --global vue-cli #安装的是2.9.x版本 |
1 | vue init webpack vue-frontend |
安装完成后会提醒你使用
1 | cd vue-frontend |
指令编译和运行VUE app,打开`http://localhost:8081/皆可查看你的网页应用
control+C
退出
完整的可用指令为
1 | install dependencies |
使用 frontend-maven-plugin
1 | <project xmlns = "http://maven.apache.org/POM/4.0.0" |
整个project的groupID
和artifactID
需要自己命名
frontend-maven-plugin`的版本可以从下面网站找到最新的
https://mvnrepository.com/artifact/com.github.eirslett/frontend-maven-plugin
Vue 调用 RESTful API
可以使用axios
工具
1 | npm audit fix npm install axios --save |
调用方法:
Calling a REST service with Axios is simple. Go into the script area of your component (e.g. in the Service.vue) and add:
1 | `import axios from 'axios' data () { return { response: [], errors: [] } }, callRestService () { axios.get(`/api/hello`) .then(response => { // JSON responses are automatically parsed. this.response = response.data }) .catch(e => { this.errors.push(e) }) } }` |
Now inside the template area of your Vue.js component you can request a service call with the callRestService()
method – and access the response data accordingly:
1 | `<button class=”Search__button” @click="callRestService()">CALL Spring Boot REST backend service</button> <h3>{{ response }}</h3>` |
还有一个问题: if we start the webpack-dev-server via npm run dev
, it will serve our web application on http://localhost:**8080**. But our Spring Boot REST backend runs on http://localhost:**8088**! As a core concept of web application security, the same-origin policy (SOP) will then prevent our Vue.js frontend from accessing its Spring Boot backend – resulting in SOP errors.
使用 element-starter-master
安装 webpack
1 | npm install webpack |
代码结构
webpack.base.conf.js
中的entry
规定了整个工程的入口js为main.js
main.js
中规定了需要在页面中调用的VUE组件App,对应到同级目录下的App.vue
webpack在编译时将.vue
文件中的html,js,css都抽出来合成新的单独的文件。
使用 Element-UI
在project目录下执行
1 | vue add element |
这行指令会让你选择上面3个?,在工程的src
下添加plugin
->element.js
加入对element的引用
修改代码
In Vue you must have only one root element in your templates.
如果.vue
文件的template下需要有多个元素,则用一个大的\
使用axios实现POST GET
登录/注册等操作需要前端将信息POST/GET给后端,需要使用axios工具
参考: https://zhuanlan.zhihu.com/p/52059361
1 | npm i axios # 安装 |