gpt4 book ai didi

routerlink 更改时不调用 Angular 2 ngOnInit

转载 作者:太空狗 更新时间:2023-10-29 19:36:20 25 4
gpt4 key购买 nike

我有一个菜单栏,其中包含从 express api 加载的菜单列表,每个菜单都引用了一个页面,该页面有一个别名,该别名将是该页面的 URL我试图在单击菜单时加载页面,它已完成,但仅在我刷新页面时这是我的菜单代码

export class MenuList implements OnInit {

menus:Menu[];
menu:Menu;
page:Page;

constructor(private router:Router,
private menuService:MenuService) {
}

getMenus():void {
this.menuService.getMenus()
.subscribe(
menus => this.menus = menus, //Bind to view
err => {
// Log errors if any
console.log(err);
}
);
}

getPage(id):void {
this.menuService.getPage(id)
.subscribe(
page => this.page = page, //Bind to view
err => {
// Log errors if any
console.log(err);
});
}

ngOnInit():void {
this.getMenus();
}

这是我的模板菜单

<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="navbar" style="background-color: #97455f">
<div class="navv">
<ul *ngFor="let menu of menus">
<li class="col-lg-1 col-md-1 col-xs-12 col-sm-12"><a [routerLink]="[menu.page.alias]">{{menu.title}}</a></li>


</ul>
</div>
</div>

</div><!-- /.navbar-collapse -->

我认为问题出在我的页面组件的 ngOnInit 上,它没有被调用

    @Component({
selector: 'page',
templateUrl: './page.component.html'
})
export class PageComponent implements OnInit {
alias: string;
page:Page;

constructor(private router:Router,
private route: ActivatedRoute,
private pageService:PageService) {

this.route.params.subscribe(
(params : Params) => {
this.alias = params["alias"];
console.log(this.alias)
}
);
}




ngOnInit():void {
debugger;
this.pageService.getPage(this.alias).subscribe(page => {
this.page = page;
console.log(this.page);
});
}

}

这是我的页面模板

    <p *ngIf="page" [innerHTML]="page.content" ></p>

这是我的应用路由代码

const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: ':alias', component: PageComponent },
{ path: 'archived', component: ArchivedComponent },
{ path: 'home', component: HomeComponent },
{ path: 'menu', component: MenuList },
{ path: 'detail/:id', component: ActualiteDetailComponent },
{ path: 'contact', component: ContactComponent },

];

@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}

这是我的应用组件

@Component({
selector: 'my-app',
template: `
<menu-list></menu-list>
<hr>
`

})
export class AppComponent {

}

Home of application with the menu bar

最佳答案

这是 Gunter 所谈论内容的代码示例:

  constructor(private route: ActivatedRoute) { }

ngOnInit() {
this.route.params.subscribe(
params => {
let id= +params['id'];
this.getProduct(id);
});
}

此代码监视参数的变化并执行箭头函数内的任何代码。

关于routerlink 更改时不调用 Angular 2 ngOnInit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45864816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com