使用java操作vue

这里我们需要使用axios,所以在终端里面先运行npm install axios --save
运行好之后的页面
index.vue

<template>
	<div>
		<h1>这里是首页</h1>
	</div>
</template>

<script>
	//引入axios(类似于ajax)
	import axios from "axios"
	
	export default{
		mounted(){
			//对就你java中请求的名称
			axios.get("/selectAll").then(
				res => {
					console.log(res);
				}
			)
		}
		
	}
</script>

<style>
</style>

在项目的根路径下创建vue.config.js(作用是对后台请求的处理简化)

在这里插入图片描述

module.exports = {
	//配置接口转发
	devServer: {
		//对应你Java项目访问的请求
		proxy: "http://127.0.0.1:8080/order"
	}
};

你访问的java中的项目请求路径就是vue.config.js的proxy的值加index.vue中axios.get()里面的值
例如我访问的就是"http://127.0.0.1:8080/order/selectAll",将前面公用部分的放到了proxy,后者就是一个一个的请求
启动后摁下f12后访问的结果
在这里插入图片描述

将list数据取出来显示

index.vue

<template>
	<div>
		<h1>这里是首页</h1>
		<Header :list="userList"></Header>
	</div>
</template>

<script>
	//引入组件
	import Header from './component/header.vue'
	import axios from "axios"
	
	export default{
		components: {
			Header
		},
		data:function(){
			return{
				userList:[]
			}
		},
		mounted(){
			axios.get("/selectAll").then(
				res => {
					console.log(res);
					const data=res.data;
					this.userList=data;
				
				}
			)
		}
		
	}
</script>

<style>
</style>

header.vue

<!--组件-->
<template>
	<div>
		<!-- <h1>这里是头部部分 {{name}}</h1> -->
		<p v-for="user in list" :key="user.id">
			{{user.userName}}
		</p>
	</div>
	
</template>

<script>
	export default{
		name:'Header',
		props:{
			list:Array
		}
	}
</script>

<style>
</style>

在这里插入图片描述

传递参数

index.vue

<template>
	<div>
		<h1>这里是首页</h1>
		<!--获取str的值并且传给组件Header的属性name-->
		<Header :name="str" :list="userList"></Header>
	</div>
</template>

<script>
	//引入组件
	import Header from './component/header.vue'
	import axios from "axios"
	
	export default{
		//一个vue实例,实例名称叫Index
		name:"Index",
		components: {
			Header
		},
		data:function(){
			return{
				str:"张三",
				/* userList:[
					{"id":1,"userName":"张三"},
					{"id":2,"userName":"李四"},
					{"id":3,"userName":"王五"}
				] */
				userList:[]
			}
		},
		mounted(){
			axios.get("/selectAll").then(
				res => {
					console.log(res);
					const data=res.data;
					this.userList=data;
				}
			)
		}
	}
</script>

<style>
</style>

header.vue

<!--组件-->
<template>
	<div>
		<!-- <h1>这里是头部部分 {{name}}</h1> -->
		<p v-for="user in list" :key="user.id">
			{{user.userName}}
		</p>
		
		<input v-model="userName" />
		<br />
		<button @click="login">提交</button>
	</div>
	
</template>

<script>
	import axios from 'axios'
	
	export default{
		name:"Header",
		data:function(){
			return{
				userName:""
			}
		},
		props:{
			list:Array,
			name:String
		},
		methods:{
			login(){
				axios.post("/login",{
					userName:this.userName
				}).then(
					res=>{
						console.log(res);
					}
				)
			}
		}
	}
</script>

<style>
</style>

controller

 @PostMapping("login")
    public String login(@RequestBody User user){
        System.out.println("getUserName:"+user.getUserName());

        return "success!!!";
    }

在这里插入图片描述

对axios的简化
在main.js中加入以下两行代码

import axios from 'axios'
Vue.prototype.$http = axios

其他页面的导入axios的内容就可以注释了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值