router.navigate()之后的Angular2 +组件更新

时间:2018-01-22 13:34:37

标签: angular

我有奇怪的问题,我不知道如何解决它。我有my-offers.component,显示公告和form.component。当我使用onOfferSubmit()时,它会将我导航到我的优惠,但不会显示添加新的通知。我必须使用F5'刷新'组件或使用导航按钮来启动my-offers.component的ngOnInit。如何使它工作?

form.component.ts

onOfferSubmit(offerForm) {
  this.option === 'add' ? this.onAddOfferSubmit(offerForm.value) : this.onEditOfferSubmit(offerForm.value);
  this._router.navigate(['profile/my-offers']);
}

MY-offers.component.ts

export class MyOffersComponent implements OnInit {

myOffersId: string[];
privateLessons: PrivateLesson[] = [];

constructor(
  private _authService: AuthService,
  private _privateLessonsService: PrivateLessonsService
) { }

ngOnInit() {
  this._authService.getProfile().subscribe(
    profile => {
      this.myOffersId = profile.user.lessonsID;

      this.privateLessons = [];

      this.myOffersId.filter(offerID => {
        this._privateLessonsService.getPrivateLessonByID(offerID).subscribe(
          privateLesson => {
            this.privateLessons.push(privateLesson);
          }
        );
      });
    },
    err => {
      console.log(err);
      return false;
    }                  
  );
}

米-Y-offers.component.html

<ul class="list">
  <li class="list__item"
      *ngFor="let privateLesson of privateLessons">
    <app-my-announcement [privateLesson]="privateLesson"></app-my-announcement>
  </li>
</ul>

编辑:

我正在从http://localhost:4200/profile/add-offer导航到http://localhost:4200/profile/my-offers

1 个答案:

答案 0 :(得分:0)

如果您导航到同一路线并且仅更改参数值您可以尝试此操作 -

您可以注入ActivatedRoute并订阅params

constructor(route:ActivatedRoute) {
  route.params.subscribe(val => {
    // put the code from `ngOnInit` here
  });
}