Angular ActivateRoute快照在TS类构造函数中不起作用

Angular ActivateRoute快照在TS类构造函数中不起作用,angular,typescript,angular2-routing,Angular,Typescript,Angular2 Routing,我正面临打字脚本和角度2的问题。 我的TS课程如下所示 'import { Component, OnInit } from '@angular/core'; import {ActivatedRoute} from '@angular/router'; @Component({ selector: 'app-blogdetail', templateUrl: './blogdetail.component.html', styleUrls: ['./blogdetail.comp

我正面临打字脚本和角度2的问题。 我的TS课程如下所示

'import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router';

@Component({
  selector: 'app-blogdetail',
  templateUrl: './blogdetail.component.html',
  styleUrls: ['./blogdetail.component.css']
})
export class BlogdetailComponent implements OnInit {

   blogId:any;
   title:any;

    constructor(private _activatedRoute:ActivatedRoute) {
    //Does not work
     // this.blogId = this._activatedRoute.snapshot.params['id'];
    //this.title= `This is blog detail for blogID:${this.blogId}`;

   }

  ngOnInit() {
      this.blogId = this._activatedRoute.snapshot.params['id'];
    this.title= `This is blog detail for blogID:${this.blogId}`;
  }

}'
我必须在ngOnit事件中获取rout参数。当我最初使用相同的代码来获取TS类构造函数中的param时,我得到的blogId变量值是未定义的。据我所知,事件的顺序与下图相似, ![流量]


我们是否必须在对象创建时始终在ngOnIt中获取activatedRoute快照值???

没有您的参数

因此,您需要在ngOnInit或ngOnInit上使用它来将其分配给变量

或者,如果您真的希望在构造函数中使用此代码,则使用observable,如下所示

@Component({...})
class MyComponent {
  constructor(route: ActivatedRoute) {
    const id: Observable<string> = route.params.map(p => p.id);
    const url: Observable<string> = route.url.map(segments => segments.join(''));
    // route.data includes both `data` and `resolve`
    const user = route.data.map(d => d.user);
  }
}
尽管我相信在构造函数中使用ActivateRoute不是一个好的实践

参考:

创建对象时,angular没有您的参数

因此,您需要在ngOnInit或ngOnInit上使用它来将其分配给变量

或者,如果您真的希望在构造函数中使用此代码,则使用observable,如下所示

@Component({...})
class MyComponent {
  constructor(route: ActivatedRoute) {
    const id: Observable<string> = route.params.map(p => p.id);
    const url: Observable<string> = route.url.map(segments => segments.join(''));
    // route.data includes both `data` and `resolve`
    const user = route.data.map(d => d.user);
  }
}
尽管我相信在构造函数中使用ActivateRoute不是一个好的实践

参考: 你说得对。 发生的情况是,当为视图启动组件时,将调用构造函数。但是,由于此时只创建了新实例,因此路由未被激活,因此无法获取路由参数。 另一方面,当您在OnInit中使用它时,路由被激活,您将获得参数。 不过,我不认为这是最好的方法,因为您应该订阅OnIIT中的PARS PARAMS来寻找PARAMs,例如:

ngOnInit(){
    this.routeParamsSub = this.route.params.subscribe(routeParams => {
        this.blogId = routeParams['id']; 

       // do something else here
    });
}
在这方面也可以看到类似的方法

你说得对。 发生的情况是,当为视图启动组件时,将调用构造函数。但是,由于此时只创建了新实例,因此路由未被激活,因此无法获取路由参数。 另一方面,当您在OnInit中使用它时,路由被激活,您将获得参数。 不过,我不认为这是最好的方法,因为您应该订阅OnIIT中的PARS PARAMS来寻找PARAMs,例如:

ngOnInit(){
    this.routeParamsSub = this.route.params.subscribe(routeParams => {
        this.blogId = routeParams['id']; 

       // do something else here
    });
}

在这方面也可以看到类似的方法

那么组件不是通过延迟加载由angular初始化的吗??因此,当我去路由时,它将被初始化,TS类构造函数被调用,或者一次加载整个模块??今天我将检查您的可观察解决方案。@user2225263延迟加载与生命周期挂钩是不同的概念。首先,启动组件,并使用构造函数创建组件的新实例。然后组件加载到导航中,路由发生,然后触发其他生命周期挂钩,如ngAfterViewInitAhh..好..我明白了。谢谢你的帮助。感谢。那么组件不是通过延迟加载由angular初始化的吗??因此,当我去路由时,它将被初始化,TS类构造函数被调用,或者一次加载整个模块??今天我将检查您的可观察解决方案。@user2225263延迟加载与生命周期挂钩是不同的概念。首先,启动组件,并使用构造函数创建组件的新实例。然后组件加载到导航中,路由发生,然后触发其他生命周期挂钩,如ngAfterViewInitAhh..好..我明白了。谢谢你的帮助。谢谢,谢谢。我现在明白了。我是按照老派的方式做的。那篇文章很有帮助。为什么要订阅路由参数而不是使用快照?在您可以从单个博客页面导航到多个博客页面的场景中,订阅参数不是唯一的好处吗?例如,如果浏览任何博客页面的唯一方法是通过博客页面ID列表,那么博客页面本身就不需要订阅,对吗?@NeilS。你完全正确。当我们必须使用不同的路由参数再次跳到活动组件上时,订阅更为有利。我们也可以按照您的建议使用快照。谢谢。我现在明白了。我是按照老派的方式做的。那篇文章很有帮助。为什么要订阅路由参数而不是使用快照?在您可以从单个博客页面导航到多个博客页面的场景中,订阅参数不是唯一的好处吗?例如,如果浏览任何博客页面的唯一方法是通过博客页面ID列表,那么博客页面本身就不需要订阅,对吗?@NeilS。你完全正确。当我们必须使用不同的路由参数再次跳到活动组件上时,订阅更为有利。我们也可以按照您的建议使用快照。