1. components -- 注册组件。
在components中注册BlogHeader组件,在template中就可直接使用
<template>
<div>
<blog-header></blog-header>
<hr/>
<div>
用户名:<input type="text" v-model="loginInfoVo.username" placeholder="请输入用户名" />
<br/>
密码:<input type="password" v-model="loginInfoVo.password" placeholder="请输入密码" />
<br/>
<button v-on:click="login">登录</button>
</div>
<br/>
<blog-footer></blog-footer>
</div>
</template>
<script>
import BlogHeader from '@/view/BlogHeader.vue'
import BlogFooter from '@/view/BlogFooter.vue'
export default {
name:"BlogLogin",
// 注册组件
components:{BlogHeader,BlogFooter},
}
</script>
===================================================
2. $axios
代码入下:
methods:{
/* login(){
this.$axios.post().then().catch()
}
*/
login(){
this.$axios.post('/login',{
username:this.LoginInfoVo.username,
password:this.LoginInfoVo.password
}).then(successResponse =>{
this.responseResult = JSON.stringify(successResponse.data)
if(successResponse.data.code === 200){
this.$router.replace({path:'/index'})
}
}).catch()
}
}
使用$asios 完成 ajax请求。
请求方法的别名
为方便起见,为所有支持的请求方法提供了别名
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])
在使用别名方法时, url
、method
、data
这些属性都不必在配置中指定。
方法
.post() -- 后台方法路径与传参
.then() -- 方法返回值,及后续操作
.catch() -- then方法里有错误代码会走catch里
3. $router
- this.$router.push
跳转到指定url路径,并想history栈中添加一个记录,点击后退会返回到上一个页面
- this.$router.replace
跳转到指定url路径,但是history栈中不会有记录,点击返回会跳转到上上个页面
- this.$router.go(n)
向前或者向后跳转n个页面,n可为正整数或负整数
// 字符串
this.$router.push('index')
// 对象
this.$router.push({path: 'login-pw'})
// 带参数
this.$router.push({path: 'login-pw', query: {'account': this.account.account}})
// 跳转后的页面获取参数
this.account.account = this.$route.query.account