Springboot+vue combination, back-end learning front-end (1)

As the saying goes, back-end engineers eventually become full-stack engineers...

Build the front-end project

Build a new vue project, see the link for details

insert image description here

insert image description here
insert image description here

Build the backend project

insert image description here
There was a little mistake here, and I couldn’t connect to the spring website. Later, I found that even this website was intermittent... I changed the url as shown in the picture... I
waited for about 10 minutes

Another problem is that the default dependency jdk is 17 for versions above spring3.0
insert image description here

Login interface development

front direction

insert image description here

Appindex

insert image description here

<template>
  <div>
    Hello World! 进入成功!
  </div>
</template>

<script>
export default {
    
    
  name: 'Appindex'
}
</script>

<style scoped>

</style>

login

<template>
  <div>
    用户名:<input type="text" v-model="loginForm.username" placeholder="请输入用户名"/>
    <br><br>
    密码: <input type="password" v-model="loginForm.password" placeholder="请输入密码"/>
    <br><br>
    <button v-on:click="login">登录</button>
  </div>
</template>

<script>
export default {
    
    
  name: 'Login',
  data () {
    
    
    return {
    
    
      loginForm: {
    
    
        username: '',
        password: ''
      },
      responseResult: []
    }
  },
  // 定义了登录按钮的点击方法,即向后端 /login 接口发送数据,获得成功的响应后,页面跳转到 /index。
  methods: {
    
    
    login () {
    
    
      this.$axios
        .post('/login', {
    
    
          username: this.loginForm.username,
          password: this.loginForm.password
        })
        .then(successResponse => {
    
    
          if (successResponse.data.code === 200) {
    
    
            this.$router.replace({
    
    path: '/index'})
          }
        })
        .catch(failResponse => {
    
    
        })
    }
  }

}
</script>

<style scoped>

</style>

front-end reverse proxy

insert image description here

import Vue from 'vue'
import App from './App'
import router from './router'
// 设置反向代理,前端请求默认发送到 http://localhost:8443/api
var axios = require('axios')
axios.defaults.baseURL = 'http://localhost:8443/api'
// 全局注册,之后可在其他组件中通过 this.$axios 发送数据
Vue.prototype.$axios = axios
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
    
    
  el: '#app',
  router,
  components: {
    
     App },
  template: '<App/>'
})

Because the new module axios is used, you need to enter the project folder and execute npm install --save axios to install this module
insert image description here

Configure page routing

src\router\index.js
insert image description here

import Vue from 'vue'
import Router from 'vue-router'
// 导入刚才编写的组件
import AppIndex from '@/components/home/AppIndex'
import Login from '@/components/Login'

Vue.use(Router)

export default new Router({
    
    
  routes: [
  // 下面都是固定的写法
    {
    
    
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
    
    
      path: '/index',
      name: 'AppIndex',
      component: AppIndex
    }
  ]
})


cross domain support

insert image description here
change into
insert image description here

    proxyTable: {
    
    
      '/api': {
    
    
        target: 'http://localhost:8443',
        changeOrigin: true,
        pathRewrite: {
    
    
          '^/api': ''
        }
      }
    }

Startup project

There is a problem,
F12 on the whiteboard to view the reason Uncaught TypeError: Cannot set properties of undefined (setting 'baseURL')
Use the method of using axios in vue3 to report an error , and finally solve it

Second solution
insert image description here

insert image description here
insert image description here
Note that the address is localhost:8081/#/login, and there is this # in the middle because Vue's routing uses the Hash mode, which is a classic usage of single-page applications and can be solved in the backend

backend page

insert image description here

insert image description here
Insert picture description here](https://img-blog.csdnimg.cn/3d91c76803434a80a72016d74e2ce48c.png)

insert image description here

test login

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/m0_54765221/article/details/129161388