下面以post请求为例:
dologin(){
this.$http.post(
"url",
{"accountname": this.accountname, "password": this.accountpsw},
{ emulateJSON: true }
).then(response => {
console.log(JSON.stringify(response.data, null, 4));
});
}
这是一个请求登录的接口(url为请求的后台地址)。
事件的触发是通过一个按钮:
<button type="button" @click="dologin">登录</button>
但是在点击之后,程序报错:TypeError: Cannot read property 'post' of undefined
原因:当前项目为新项目,并没有引入依赖:
在项目下使用命令 npm i vue-resource
然后在项目中添加下面两行代码:
import vueResource from 'vue-resource'
Vue.use(vueResource)
到这边,一个请求基本就成功了。