用Vue的双向绑定和 element ui的组件工具可以快速的在spring项目开发前端内容,可以说真的是学Java萌新的福音了。
全代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
<script src="js/jquery-3.6.3.js.js"></script>
<script src="js/vue.js"></script>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="appVue">
<el-form ref="" :model="form" label-width="80px">
<el-form-item label="用户名:">
<el-input v-model="form.username" style="width: 200px"></el-input>
</el-form-item>
<el-form-item label="密码:">
<el-input v-model="form.password" style="width: 200px"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="login">登录</el-button>
<el-button>注册</el-button>
</el-form-item>
</el-form>
</div>
</body>
<script>
let appVue = new Vue({
el: "#appVue",
data: {
form: {
username: '',
password: '',
},
},
methods: {
login() {
window.location.href='buildings.html'
alert("登录成功")
},
reg(){
window.location.hred='reg.html'
}
}
})
</script>
</html>
步骤主要是简化为三部:
导入相关的配置文件
<script src="js/jquery-3.6.3.js.js"></script>
<script src="js/vue.js"></script>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
使用element ui创建页面:
<div id="appVue">
<el-form ref="" :model="form" label-width="80px">
<el-form-item label="用户名:">
<el-input v-model="form.username" style="width: 200px"></el-input>
</el-form-item>
<el-form-item label="密码:">
<el-input v-model="form.password" style="width: 200px"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="login">登录</el-button>
<el-button>注册</el-button>
</el-form-item>
</el-form>
</div>
注意最大的div是用来创建Vue应用的。
最后写script就好了
let appVue = new Vue({
el: "#appVue",
data: {
form: {
username: '',
password: '',
},
},
methods: {
login() {
window.location.href='buildings.html'
alert("登录成功")
},
reg(){
window.location.hred='reg.html'
}
}
})
el是用id选择锁定了div
data是应用内的各种数据
methods顾名思义是所有方法。
页面效果如下: