比如说
var vm = new Vue({
el:"#app",
data:{
notices:[],
id:"",
title:"",
content:"",
type:1,
status:0,
pageCurrent:1,
pageCount:0
},
methods:{
doGetNotices(){
let title = document.getElementById("titleId").value;
pageCurrent=${this.pageCurrent}&title=${title}`;
let url=`http://localhost/notice/`;
axios.get(url,{
params: {
pageCurrent: this.pageCurrent,
title:title
}
}).then(function (response) {
let jsonResult=response.data;
this.vm.notices = jsonResult.data.list
console.log(jsonResult);
this.vm.pageCurrent=jsonResult.data.pageNum;
this.vm.pageCount=jsonResult.data.pages;
})
}
}
})
这样的一串代码中为啥pageCurrent有的是this.vm.pageCurrent,有的又直接this.pageCurrent呢?
.then
中的this
指向全局对象,在浏览器中就是window
。所以这里的
this.vm
即window.vm
,指的是上面声明的那个var vm
。这种写法是 Garbage,可移植性很差——这段代码像这样套在函数里就不行了:
正常使用的
this
(即不需要用this.vm
就能访问属性的那些)就是指的vm
。