2017-09-29 44 views
0

我想從服務器角數據2應用程序中獲取數據,但我在某處做某事可能不是從服務器加載數據。這不會將我的數據加載到表中。角材2表從服務器獲取數據

userComponent.ts

@Component({ 
    selector: 'app-user', 
    templateUrl: './user.component.html', 
    styleUrls: ['./user.component.css'] 
}) 
export class UserComponent { 
    displayedColumns = ['id', 'firstName', 'lastName', 'email']; 
    dataSource: UserDataSource; 

    constructor(private userSrvc: UserService) { } 

    ngOnInit() { 
     this.dataSource = new UserDataSource(this.userSrvc); 
    } 

    logPage(length) { 
     console.log(length); 
    } 
} 

export class UserDataSource extends DataSource<User> { 
    userSrvc: any; 
    length = 20; 
    data: Observable<User[]>; 

    /** Connect function called by the table to retrieve one stream containing the data to render. */ 
    constructor(userService) { 
     super(); 
     this.userSrvc = userService; 
    } 

    connect(): Observable<User[]> { 

     this.userSrvc.get().subscribe(users => { 
      this.data = users; 
     }) 
     return this.data; 
    } 

    disconnect() { } 
} 

,如果我嘗試connect方法裏面返回它給我的錯誤控制檯日誌。

這裏是我的UserService.ts我在那裏調用用戶API來獲取數據。

@Injectable() 
export class UserService { 

    constructor(private http: Http) { } 

    get(): Observable<User[]> { 
    let header: Headers = new Headers({ 'Content-Type': 'application/json' }); 
    let options: RequestOptions = new RequestOptions({ headers: header }); 
    return this.http.get(`${BASE_URL}user/get/`, options) 
     .map((response: Response) => { 
     return <User[]>response.json(); 
     }).catch(this.errorHandler); 
    } 

    errorHandler(error: Response) { 
    return Observable.throw(`Error Message: ${error.statusText}`); 
    } 

} 

回答