vuejs之vue和springboot后端进行通信

前端开发 作者: 2024-08-26 09:40:01
一、新建一个vue项目,建立好后的相关文件 查看一下新建好的vue项目的结构: 当前各个文件中的内容: App.vue:主入口 <template> <div id="ap
<template>
  div id="app">
    ="nav">
      router-link to="/">Home</router-link> |
      ="/about">Aboutdivrouter-view/>
  >
>

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;
}
>
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

Vue.config.productionTip = false

new Vue({
  router,store,render: h => h(App)
}).$mount('#app')
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',name: 'Home''About'// route level code-splitting
     this generates a separate chunk (about.[hash].js) for this route
     which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router =  VueRouter({
  mode: 'history'default router
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default  Vuex.Store({
  state: {
  },mutations: {
  },actions: {
  },modules: {
  }
})
class="about"h1>This is an about page>
="home"img alt="Vue logo" src="../assets/logo.png"HelloWorld msg="Welcome to Your Vue.js App"script
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'

export default {
  name: 'Home',components: {
    HelloWorld
  }
}
>
<!--只允许有一个根节点-->
  ="test"tabletr>
        td>编号>姓名>年龄>性别>邮箱>爱好>自我介绍tr v-for="(item,index) in users"
          :key="index">{{item.id}}>{{item.username}}>{{item.age}} >{{item.gender}}>{{item.email}}>{{item.hobby}}>{{item.introduce}}
export default {
  name: "Book",data () {
    return {
      msg: "hello world",users: {},}
  },created () {
    const that = this;
    axios.get('http://localhost:8181/user/findAll/')
      .then(function (response) {
        console.log(response);
        that.users = response.data;
      })
  }
}
style scoped>
import Vue from 'vue'
import Home from '../views/Home.vue'
import Test from '../views/Test.vue' [
  {
    path: '/test',name: 'Test',component: Test,},
  {
    path: '/'default router
 |
      <router-link to="/test">Test</router-link>
    >
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
  jpa:
    #控制台显示SQL
    show-sql: true
    properties:
     hibernate:
      format_sql: true
server:
  port: 8181
package com.gong.springbootvue.entity;

import lombok.Data;

 javax.persistence.Entity;
 javax.persistence.Id;

@Entity
@Data
public  User {
    @Id
    private Integer id;
     String username;
     Integer age;
     Integer gender;
     String email;
     String hobby;
     String introduce;

}
 com.gong.springbootvue.repository;

 com.gong.springbootvue.entity.User;
 org.springframework.data.jpa.repository.JpaRepository;

interface UserRepository  extends JpaRepository<User,Integer> {
}
 com.gong.springbootvue.controller;

 com.gong.springbootvue.repository.UserRepository;
 org.springframework.beans.factory.annotation.Autowired;
 org.springframework.stereotype.Controller;
 org.springframework.web.bind.annotation.RequestMapping;
 org.springframework.web.bind.annotation.ResponseBody;

 java.util.List;

@Controller
@RequestMapping("/user")
 UserController {

    @Autowired
    UserRepository userRepository;

    @ResponseBody
    @RequestMapping("/findAll")
    public List<User> getAll(){
        return userRepository.findAll();
    }

}
 com.gong.springbootvue.config;

 org.springframework.context.annotation.Configuration;
 org.springframework.web.servlet.config.annotation.CorsRegistry;
 org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
class VueConfig implements WebMvcConfigurer{
    @Override
    void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
}
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68902.html