使用get请求:
export default {
data(){
return{
form:{
username:'',
password:''
}
},
//组件你被创建的时候会执行created生命周期函数
created() {
this.getMenuList()
},
methods:{
//表单提交时进行验证
async getMenuList() {
//在mian.js中已经通过Vue.prototype.$http=axios,将axios包挂载到vue的原型对象上
// 所以可以通过this.$http发起ajax请求
const { data: res } = await this.$http.get(
'请求路径', { params: this.form }
)
//{ params: this.queryInfo } 是请求参数
//注:要发送请求,要先保证数据库和接口的打开
if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
this.menulist = res.data
console.log(res)
}
使用post发送请求:
this.$http.post('请求路径',this.form)
区别:get接收的参数用对象包裹起来,post接收的参数直接使用不需要包裹