按照教程,我正在开发我的拳头应用程序。在这个应用程序中,我想实现滑动导航。 但是我遇到了一个问题:我无法访问我的组件对象。
我正在使用NativeScript + Angular。
constructor(
private _router: Router
, private _routeParams: RouteParams
, private _signalListService: SignalListService
, private _page : Page
) {
console.log('ViewPageConstructor : ' + this);
this.itemId = this._routeParams.get("id");
this._page.on("swipe", function(args: gestures.SwipeGestureEventData) {
console.log("Swipe Direction From event function: " + args.direction);
console.log(args); // [object Object]
console.log(_page); // Page(148)
console.log(this); // undefined
this.swipe(args); // Nothing happen ... no error ...
});
}
swipe(args: gestures.SwipeGestureEventData) {
console.log("Swipe Direction From event function: " + args.direction);
this._router(...
}
我从未接受滑动功能。命令日志中没有错误...
由于
答案 0 :(得分:2)
您的滑动事件中的闭包.. 此不是来自构造函数的 this 。 你能做什么来“缓存”你的构造函数引用:
export class AppComponent {
constructor(private page: Page) {
var that = this;
this.page.on("swipe", function(args: gestures.SwipeGestureEventData) {
console.log("Swipe Direction From event function: " + args.direction);
that.onSwipe();
})
}
public onSwipe() {
console.log("onSwipe triggered");
}
}