問題結構
類似於嵌套函數,function movePlane方法内部使用了this.app,注意這樣寫的this可不是vue實例,會報異常找不到app
export default {
data() {
return {
app:null,
}
},
methods: {
// 游戏窗体加载
initPixi() {
function movePlane(event) {
if (isStart) {
var pos = event.data.getLocalPosition(this.app.stage)
plane.x = pos.x
plane.y = pos.y - 70
}
}
}
問題解決
movePlane = movePlane.bind(this);
使用bind方法,把this綁定到movePlane的方法上
export default {
data() {
return {
app:null,
}
},
methods: {
// 游戏窗体加载
initPixi() {
function movePlane(event) {
if (isStart) {
var pos = event.data.getLocalPosition(this.app.stage)
plane.x = pos.x
plane.y = pos.y - 70
}
}
movePlane = movePlane.bind(this);
}