学习Vue+spring boot简单前后端分离笔记

后端部分

springboot

节省时间此次学习仅使用controller层

maven引入 fastjson

		<!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version> 1.2.54</version>
        </dependency>

TestController

使用@restController返回json

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

//返回json格式控制器
@RestController
public class TestController {
    //跨域
    @CrossOrigin
    @RequestMapping(value = "/t3")
    public JSON getUser(@RequestParam(value="userId") int userId){

        String name="haoahohoa";
        int Id=userId;
        int password=123456;

        Map<Object,Object> map=new HashMap<>();
        map.put("Id",Id);
        map.put("name",name);
        map.put("password",password);
        JSONObject res=new JSONObject();
        res.put("result",map);
        return res;
    }
}

因为前后端分开部署,后端地址loaclhost:8080,前端地址localhost:8081,所以会产生跨域问题。前端调用后端时会爆红。
在这里插入图片描述
解决跨域问题有很多方法,这里偷懒采用springboot 的@CrossOrigin注解解决。

在这里插入图片描述后端完成使用ApiPost测试

前端部分

Vue+axios

使用vuecli vue3快速搭建前端
学习测试目录
学习测试目录结构,此次学习只用到FindUser.vue+App.vue.

1.前期准备

安装router

使用router作为前端路由

使用vuecli高版本可以在vue ui中手动引入,也可以npm引入

npm install vue-router --save

安装axios

使用axios作为前后端通信

npm install axios

补充:引入vue- axios可解决下面问题

npm install axios vue-axios

将router,axios引入vue

main.js

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

/*
import axios from "axios"
这里我引入没效果不知道什么原因所以后面直接在组件里引入
*/


const app=createApp(App);
app.use(router);
/*
app.config.globalProperties.$axios=axios;
//这里我引入没效果不知道什么原因所以后面直接在组件里引入
*/

app.mount('#app');

补充:

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios' // 导入 axios
import VueAxios from 'vue-axios' // 导入 vue-axios


const app=createApp(App);

app.use(router);
app.use(VueAxios, axios);


app.mount('#app');

2.1建立FindUser组件

FindUser.vue

<template xmlns:v-bind="http://www.w3.org/1999/xhtml">
  <div id="test">
    <div id="name">{{result.name}}</div>
    <div id="password">{{result.password}}</div>
    <div id="userId" >{{result.Id}}</div>
  </div>
</template>

<script>

//引入axios
import axios from "axios";

export default {
  name: "FindUser",

  data(){
    return {
    //后端传回来的格式
      result:{
        name:'',
        password:'',
        Id:''
      }
      }
    },
    //created()当切换到该组件时调用
  created() {
    axios.get("http://localhost:8080/t3",{
    //前端向后端传参
        params:{
          userId:7
        }
    }).then(res=>{
    //调用返回值res修改前端
      this.result.password=res.data.result.password;
      this.result.name=res.data.result.name;
      this.result.Id=res.data.result.Id
      
      console.log(res,"相应信息")
    })
  }

}

</script>

<style scoped>

</style>

2.2将FindUser引入App.vue

App.vue

<template>
  <nav>
  //路由
    <router-link to="/">Home</router-link> |
    <router-link to="/findUser">Find</router-link>
  </nav>
  //路由显示
  <router-view/>
</template>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

nav {
  padding: 30px;
}

nav a {
  font-weight: bold;
  color: #2c3e50;
}

nav a.router-link-exact-active {
  color: #42b983;
}
</style>

启动vue

在这里插入图片描述


后续

修改前端实现数据绑定传递

增加表单input

 <div>
    <input v-model="result.Id">
    <button v-on:click="setUser"></button>
  </div>

将created()修改为methods提交表单

methods:{
    setUser(){
      axios.get("http://localhost:8080/t3",{
        params:{
          userId:this.result.Id
        }
      }).then(res=>{
        this.result.password=res.data.result.password;
        this.result.name=res.data.result.name;
        this.result.Id=res.data.result.Id

        console.log(res,"相应信息")
      })
    }
  }

修改后

<template xmlns:v-bind="http://www.w3.org/1999/xhtml">
  <div>
    <input v-model="result.Id">
    <button v-on:click="setUser"></button>
  </div>
  <div id="test">
    <div id="name">{{result.name}}</div>
    <div id="password">{{result.password}}</div>


    <div id="userId" >{{result.Id}}</div>
  </div>
</template>

<script>

import axios from "axios";

export default {
  name: "FindUser",

  data(){
    return {
      result:{
        name:'',
        password:'',
        Id:''
      }
      }
    },
  methods:{
    setUser(){
      axios.get("http://localhost:8080/t3",{
        params:{
          userId:this.result.Id
        }
      }).then(res=>{
        this.result.password=res.data.result.password;
        this.result.name=res.data.result.name;
        this.result.Id=res.data.result.Id

        console.log(res,"相应信息")
      })
    }
  }

}

</script>

<style scoped>

</style>

后端做一点小修改

修改参数类型int->string

@CrossOrigin
    @RequestMapping(value = "/t3")
    //修改参数类型int->string
    public JSON getUser(@RequestParam(value="userId") String userId){

        String name="haoahohoa";
        //Id也要更改
        int Id=Integer.valueOf(userId);
        int password=123456;

        Map<Object,Object> map=new HashMap<>();
        //为体现Id已被修改,返回值Id+1
        map.put("Id",Id+1);
        map.put("name",name);
        map.put("password",password);
        JSONObject res=new JSONObject();
        res.put("result",map);
        return res;


    }

效果

提交前
在这里插入图片描述
输入参数还未提交
在这里插入图片描述
提交后
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值