Javascript angular2中的typescript,如何通过它访问类变量

Javascript angular2中的typescript,如何通过它访问类变量,javascript,angular,typescript,this,Javascript,Angular,Typescript,This,给定此代码,如何访问对象“会话”?由于“this”为空,因此失败: /// <reference path="chrome/chrome-app.d.ts" /> import { Component, Input, OnInit } from '@angular/core'; import { AppService } from './app.service'; @Component({ selector: 'tabs', templateUrl: './tem

给定此代码,如何访问对象“会话”?由于“this”为空,因此失败:

/// <reference path="chrome/chrome-app.d.ts" />

import { Component, Input, OnInit } from '@angular/core';
import { AppService } from './app.service';

@Component({
    selector: 'tabs',
    templateUrl: './templates/app.html',
    providers: [ AppService ]
})

export class AppComponent implements OnInit {
    public sessions : Object;
    constructor( private appService : AppService ) {}

    getBookmarkLists() {
        console.log(this.sessions) // it gives undefined
        this.sessions['test'] = 'yea'; // it fails
        this.appService.getBookmarks().then(function(bookmarks : any) {
            console.log(this.sessions) // it fails
        });

    }

    ngOnInit() {
        this.getBookmarkLists();
    }
 }
//
从“@angular/core”导入{Component,Input,OnInit};
从“/app.service”导入{AppService};
@组成部分({
选择器:“制表符”,
templateUrl:“./templates/app.html”,
提供商:[AppService]
})
导出类AppComponent实现OnInit{
公开会议:目标;
构造函数(私有appService:appService){}
getBookmarkLists(){
console.log(this.sessions)//它提供未定义的
this.sessions['test']='yea';//失败
this.appService.getBookmarks().then(函数(bookmarks:any){
console.log(this.sessions)//失败
});
}
恩戈尼尼特(){
这个.getBookmarkLists();
}
}

我希望能够访问变量并填充它。

您没有在任何地方初始化此
会话
对象,据我所知:

export class AppComponent implements OnInit {
    public sessions: Session[] = []; // You forgot here to initialize it
    constructor( private appService : AppService ) {}

    getBookmarkLists() {
        console.log(this.sessions) // no it shouldn't give undefined
        this.sessions['test'] = 'yea'; // and this shouldn't fail
        this.appService.getBookmarks().then((bookmarks : any) => {
            // this should be an arrow function or function bound to use it
            // otherwise this will point to the function itself.
            console.log(this.sessions) // it shouldn't fail
        });

    }

    ngOnInit() {
        this.getBookmarkLists();
    }
}
使用
sessions=[]是关键部分。
因此,这不仅仅是
这个
在方法中引用类实例的问题


传递给
then
的回调应该是一个箭头函数而不是经典函数,以保持
对类实例的引用。

您没有在任何地方初始化此
会话
对象,据我所知:

export class AppComponent implements OnInit {
    public sessions: Session[] = []; // You forgot here to initialize it
    constructor( private appService : AppService ) {}

    getBookmarkLists() {
        console.log(this.sessions) // no it shouldn't give undefined
        this.sessions['test'] = 'yea'; // and this shouldn't fail
        this.appService.getBookmarks().then((bookmarks : any) => {
            // this should be an arrow function or function bound to use it
            // otherwise this will point to the function itself.
            console.log(this.sessions) // it shouldn't fail
        });

    }

    ngOnInit() {
        this.getBookmarkLists();
    }
}
使用
sessions=[]是关键部分。
因此,这不仅仅是
这个
在方法中引用类实例的问题


传递给
then
的回调应该是一个箭头函数,而不是一个经典函数,以保持
这个
引用到类实例。

是的,就是这样,现在我可以访问会话(但显然不能访问会话的内容,但我想说这与这个问题无关,非常感谢!)是的,就是这样,现在我可以访问会话(但显然不能访问它们的内容,但我要说这与这个问题无关,非常感谢!)