我是Angular的初学者。我的目标是创建一个仅包含开始目录的文件系统。第一次调用该组件时,它会从后端加载数据。第二次导航到子目录时,我执行console.log()
来输出从路线获取的ID,它是正确的ID。
获取参数:
this.router.params.subscribe(
(params: Params) => {
this.parentEntryId = +params['id'];
console.log('directory ' + params.id);
}
);
我为什么不能到达:
console.log("foo")
我所有的代码:
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Entry } from 'app/models/entry/entry.model';
import { EntryService } from 'app/service/entry.service';
import { AuthenticationService } from '../../service/authentication.service';
import { ActivatedRoute, Params } from '@angular/router';
declare interface TableData {
headerRow: string[];
entries: Entry[];
}
@Component({
selector: 'app-directory',
templateUrl: './directory.component.html',
styleUrls: ['./directory.component.css']
})
export class DirectoryComponent implements OnInit {
tableData1: TableData;
currentUserId: number;
parentEntryId : number;
constructor(
// private route: Router,
private router: ActivatedRoute,
private entryService: EntryService,
private authenticationService: AuthenticationService
) { }
ngOnInit() {
this.currentUserId = this.authenticationService.getLoggedUserId();
this.tableData1 = {
headerRow: ['ID', 'TYPE', 'EXECUTABLE', 'NAME', 'OWNER', 'OPERETION'],
entries: []
};
this.router.params.subscribe(
(params: Params) => {
this.parentEntryId = +params['id'];
console.log('directory ' + params.id);
}
);
console.log("foo");
console.log('parentId' + this.parentEntryId);
this.entryService.listWithId(this.parentEntryId).subscribe(
(data: Entry[]) => {
console.log('enter here ');
this.tableData1.entries = data;
}
);
}
}