初始化处理

配置router目录下的index.js

  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. import Login from '@/views/Login'
  4. Vue.use(VueRouter)
  5. const routes = [
  6. {
  7. path: '/',
  8. name: 'Login',
  9. component: Login
  10. },
  11. ]
  12. const router = new VueRouter({
  13. routes
  14. })
  15. export default router

入口文件main.js

  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. import router from './router'
  4. import ElementUI from 'element-ui';
  5. import 'element-ui/lib/theme-chalk/index.css';
  6. Vue.config.productionTip = false
  7. Vue.use(ElementUI);
  8. new Vue({
  9. router,
  10. render: h => h(App)
  11. }).$mount('#app')

登录页面编写

  1. <template>
  2. <div id="root">
  3. <el-form ref="loginForm" :rules="rules" :model="form" id="login-form">
  4. <h2>Login Page</h2>
  5. <el-form-item prop="username">
  6. <el-input v-model="form.username" type="text" placeholder="Please input your phone"></el-input>
  7. </el-form-item>
  8. <el-form-item prop="password">
  9. <el-input v-model="form.password" type="password" placeholder="Please input your password"></el-input>
  10. </el-form-item>
  11. <el-form-item>
  12. <el-input v-model="form.code" style="width: 60%" placeholder="Please input check code"></el-input>
  13. <img src="" alt="">
  14. </el-form-item>
  15. <el-button type="primary" style="width: 100%" plain @click="submitForm">TO LOGIN</el-button>
  16. </el-form>
  17. </div>
  18. </template>
  19. <script>
  20. export default {
  21. data(){
  22. return{
  23. form: {
  24. username: '',
  25. password: "",
  26. code: "",
  27. },
  28. rules: {
  29. username: [{
  30. required: true,
  31. message: 'Please input your username',
  32. trigger: 'blur',
  33. },
  34. {
  35. min: 5,
  36. max: 10,
  37. message: 'Length should be 5 to 10',
  38. trigger: 'blur',
  39. }
  40. ],
  41. password: [{
  42. required: true,
  43. message: 'Please input your password',
  44. trigger: 'blur',
  45. },
  46. {
  47. min: 5,
  48. max: 10,
  49. message: 'Length should be 5 to 10',
  50. trigger: 'blur',
  51. }
  52. ],
  53. }
  54. }
  55. },
  56. methods:{
  57. submitForm() {
  58. this.$refs.loginForm.validate((valid) => {
  59. if (valid) {
  60. alert('submit!')
  61. } else {
  62. this.$message({
  63. showClose: true,
  64. message: 'Please input all the information of the form',
  65. type: 'error'
  66. });
  67. return false
  68. }
  69. })
  70. },
  71. }
  72. }
  73. </script>
  74. <style scoped lang='less'>
  75. #login-form{
  76. margin:150px auto;
  77. width: 450px;
  78. background: white;
  79. border: 1px solid #EEE;
  80. border-radius: 20px;
  81. padding: 30px 40px 60px 40px;
  82. box-shadow: 0 0 25px #CCC;
  83. h2 {
  84. text-align: center;
  85. }
  86. .el-form-item{
  87. margin-bottom: 30px;
  88. }
  89. }
  90. </style>

后端跨域解决方案

跨域含义:前端代码放在一个tomcate服务器里面,后端代码放在一个tomcat服务器里面,每个服务器都需要一个端口号,两个端口号不能很好的统一起来访问
解决方法:可以在后端解决,也可以放在前端解决,前端解决是要放在nginx里面访问的
这里面主要讲解后端解决方式

  1. @Configuration
  2. public class WebConfig extends WebMvcConfigurerAdapter {
  3. // 这是要解决跨域问题(全局配置类)
  4. @Override
  5. public void addCorsMappings(CorsRegistry registry) {
  6. registry.addMapping("/**")
  7. .allowedOriginPatterns("http://locathost")
  8. .allowedMethods("GET","POST","PUT","DELETE","OPTIONS")
  9. .allowCredentials(true)
  10. .maxAge(3600);
  11. }
  12. }

可以在后端里面创建一个专门的util工具类,在里面专门解决跨域问题的代码
本方式就是按照此种方式来进行
说明:配置类要继承WebMvcConfigurerAdapter ,在该方法下面有一个addCorsMappings方法要自己配置
allowedOriginPatterns:允许要访问的路径
allowedMethods:允许访问的方式,主要是五类,GET POST PUT DELETE 以及OPTIONS
allowCredentials:表示是否携带token的值,设置为允许
maxAge:允许最大的访问时间

  1. @RestController
  2. @CrossOrigin("http://localhost:8080")
  3. public class LoginController {
  4. @RequestMapping("/login")
  5. public String login(@RequestBody User user){
  6. System.out.println("user:"+user);
  7. return "login";
  8. }
  9. }

封装axios工具类

Vue-Springboot前后端分离项目实战 - 图1

引入iconfont图标

网站:https://www.iconfont.cn/

Vue-Springboot前后端分离项目实战 - 图2

下载到本地放到asset中,在main.js中引入

  1. import './assets/font/iconfont.css'

注意:prefix-icon中类不要加点

  1. <el-form-item prop="username">
  2. <el-input v-model="form.username" type="text" prefix-icon="iconfont icon-shouye" placeholder="Please input your phone"></el-input>
  3. </el-form-item>
  4. <el-form-item prop="password">
  5. <el-input v-model="form.password" type="password" prefix-icon="iconfont icon-quanxianmokuai" placeholder="Please input your password"></el-input>
  6. </el-form-item>

日常注意点

注意Post请求和Get请求的区别