如何创建一个Vue工程
- 前提条件,安装[Node.js](Node.js (nodejs.org))。
- 安装完成后,选择要创建项目的路径,启动cmd,执行命令:
npm init vue@latest
- 上述指令将会安装并执行create-vue,成功后将会得到如下可选功能提示:
✔ Project name: … <your-project-name>
✔ Add TypeScript? … No / Yes
✔ Add JSX Support? … No / Yes
✔ Add Vue Router for Single Page Application development? … No / Yes
✔ Add Pinia for state management? … No / Yes
✔ Add Vitest for Unit testing? … No / Yes
✔ Add Cypress for both Unit and End-to-End testing? … No / Yes
✔ Add ESLint for code quality? … No / Yes
✔ Add Prettier for code formatting? … No / Yes
Scaffolding project in ./<your-project-name>...
Done.
-
第一行,填写你要创建的工程名,第四行Vue Router选择yes,其他项根据你使用情况,自行选择。
-
cd 项目名,进入工程,执行命令:
npm install element-plus --save
-
执行结束后,打开IDEA,将项目导入。
-
创建模板Login.vue和Main.vue
<template>
<div class="loginbody">
<div class="logindata">
<div class="logintext">
<h2>瑶华博客</h2>
</div>
<div class="formdata">
<el-form ref="form" :model="form" :rules="rules">
<el-form-item prop="username">
<el-input
v-model="form.username"
clearable
placeholder="请输入账号"
></el-input>
</el-form-item>
<el-form-item prop="password">
<el-input
v-model="form.password"
clearable
placeholder="请输入密码"
show-password
></el-input>
</el-form-item>
</el-form>
</div>
<div class="tool">
<div>
<el-checkbox v-model="checked" @change="remenber"
>记住密码</el-checkbox
>
</div>
<div>
<span class="shou" @click="forgetpas">忘记密码?</span>
</div>
</div>
<div class="butt">
<el-button type="primary" @click.native.prevent="login('form')"
>登录</el-button
>
<el-button class="shou" @click="register">注册</el-button>
</div>
</div>
</div>
</template>
<script>
export default {
name: "login",
data() {
return {
form: {
password: "",
username: "",
},
checked: false,
rules: {
username: [
{ required: true, message: "请输入用户名", trigger: "blur" },
{ max: 10, message: "不能大于10个字符", trigger: "blur" },
],
password: [
{ required: true, message: "请输入密码", trigger: "blur" },
{ max: 10, message: "不能大于10个字符", trigger: "blur" },
],
},
};
},
mounted() {
if(localStorage.getItem("news")){
this.form=JSON.parse(localStorage.getItem("news"))
this.checked=true
}
},
methods: {
login(form) {
this.$refs[form].validate((valid)=>{
if(valid){
this.$router.push("/main");
}else {
this.dialogVisible=true;
return false;
}
})
// this.$refs[form].validate((valid) => {
// if (valid) {
// login(this.form)
// .then((res) => {
// if (res.code === 200) {
// setToken(res.data.token);
// localStorage.setItem("USERNAME", res.data.username);
// this.$message({
// message: "登录成功啦",
// type: "success",
// showClose: true,
// });
// this.$router.replace("/");
// this.$router.push("/main");
// } else {
// this.$message({
// message: "账户名或密码错误",
// type: "error",
// showClose: true,
// });
// }
// })
// .catch((err) => {
// this.$message({
// message: "账户名或密码错误",
// type: "error",
// showClose: true,
// });
// });
// } else {
// return false;
// }
// });
},
remenber(data){
this.checked=data
if(this.checked){
localStorage.setItem("news",JSON.stringify(this.form))
}else{
localStorage.removeItem("news")
}
},
forgetpas() {
this.$message({
type:"info",
message:"功能尚未开发额😥",
showClose:true
})
},
register() {},
},
};
</script>
<style scoped>
.loginbody {
width: 100%;
height: 100%;
min-width: 1000px;
background-image: url("../assets/login1.jpg");
background-size: 100% 100%;
background-position: center center;
overflow: auto;
background-repeat: no-repeat;
position: fixed;
line-height: 100%;
padding-top: 150px;
}
.logintext {
margin-bottom: 20px;
line-height: 50px;
text-align: center;
font-size: 30px;
font-weight: bolder;
color: white;
text-shadow: 2px 2px 4px #000000;
}
.logindata {
width: 400px;
height: 300px;
transform: translate(-50%);
margin-left: 50%;
}
.tool {
display: flex;
justify-content: space-between;
color: #606266;
}
.butt {
margin-top: 10px;
text-align: center;
}
.shou {
cursor: pointer;
color: #606266;
}
/*ui*/
/* /deep/ .el-form-item__label {
font-weight: bolder;
font-size: 15px;
text-align: left;
}
/deep/ .el-button {
width: 100%;
margin-bottom: 10px;
} */
</style>
<template>
<el-container style="height: 500px; border: 1px solid #eee">
<el-aside width="200px" style="background-color: rgb(238, 241, 246)"