【问题标题】:page not redirecting using routerLink页面未使用 routerLink 重定向
【发布时间】:2017-10-15 09:11:26
【问题描述】:

我有这个路由

{
    path: ':lang',
    children: [
{
            path: 'page',
            children: [
                {
                    path: '',
                    component: PageComponent
                },
                {
                    path: ':Id',
                    component: PageComponent
                }
            ]
        },
...
}];

我的组件看起来像这样:

export class PageComponent implements OnInit {
busy: any;
pages: Page[];
src: any;

constructor(private httpCall: HttpCall, private router: Router, private activedRoute: ActivatedRoute, public languageService: LanguageServiceService, private http: Http) {

}

ngOnInit() {
    let Id = this.activedRoute.snapshot.params['Id'];

    this.busy = this.httpCall.get('/pub/page/GetPageById/' + Id)
        .subscribe(
        data => {
            this.pages = <Page[]>data;
        })

}

}

例如当我第一次访问时

domain.com/en/page/13

它会正常重定向,但如果我尝试重定向到

domain.com/en/page/14

从那里它会更改 url,但在页面内所有内容都保持原样。

我认为这是因为 ngOnInit 只呈现一次,直到我进入另一个 url(页面外)

我该如何解决?

【问题讨论】:

标签: angular typescript


【解决方案1】:

快照将返回单个值,因此您必须订阅每次路由“domain.com/en/page/”更改时解析的“this.activedRoute.params”可观察对象。

import {Params} from "@angular/router";

export class PageComponent implements OnInit {
busy: any;
pages: Page[];
src: any;

constructor(private httpCall: HttpCall, private router: Router, private activedRoute: ActivatedRoute, public languageService: LanguageServiceService, private http: Http) {

}

ngOnInit() {
    this.activedRoute.params.subscribe( (params: Params)=>{
        let iD = params['Id'];
        this.busy = this.httpCall.get('/pub/page/GetPageById/' + Id)
        .subscribe(
        data => {
            this.pages = <Page[]>data;
        })
     });



}

【讨论】:

    猜你喜欢
    • 2019-06-23
    • 2020-09-08
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 2021-03-20
    • 1970-01-01
    相关资源
    最近更新 更多