发表于: 2019-11-06 19:43:09
0 980
1.获取用户输入 ,并绑定onclick事件实现登录,
第一步 body添加input 并绑定登录事件
<input v-model="username" type="text" class="username" @click="login" placeholder="请输入">
第二步 初始化数据
data() {
return {
username: "",
password: "",
};
},
第三步 添加登录函数(方法)
methods: {
login() {
if (this.username === "") {
this.$toast("请输入账号");
return;
}
if (this.password === "") {
this.$toast("请输密码");
return;
}
//通过封装好的this.$req 获取参数并进行路由跳转
this.$req("/auth/login", "post", {
phone: this.username,
password: this.password
}).then(res => {
if (res.code == 1) {
this.$toast("登录成功");
this.$router.push("/");
} else {
this.$toast(res.msg);
}
});
}
}
2.页面间跳转并传参
上一个页面中,按钮处添加onclick事件 (@click=“goOther”)
script
传参并跳转
goOther(id) {
this.$router.push({ name: "url", query: { id: id } });
},
下一个页面中,设置初始化参数接收id,并渲染
//初始化
data() {
return {
id: this.$route.query.id,
info: "",
};
},
//接收参数
created() {
this.getInfo();
},
//接收参数函数
methods: {
getInfo() {
this.$req("url", "post", {
id: this.id
}).then(res => {
this.info = res.data;
});
}
}
评论