【问题标题】:Angular 5 - Constructor and ngOnInit are not executedAngular 5 - 构造函数和 ngOnInit 未执行
【发布时间】:2017-12-14 11:51:15
【问题描述】:

我将 Angular 5 与 SortableJs 结合使用。

在一个页面上,我列出了多个分组的卡片。

HTML 看起来像

<div class="group" *ngFor="let group of groups" [sortablejs]="group.cards" [sortablejsOptions]="sortableOptions">
    <div class="card" *ngFor="let card of group.cards">
        <button routerLink="card/edit/{{card.id}}">Edit</button>
    </div>
</div>

当我将一张卡片从一个组移动到另一个组并单击编辑按钮后,它会将我重定向到可以编辑所选卡片的页面。

当它把我重定向到那个页面时,构造函数和 ngOnInit 没有被执行。

点击一个元素后,这两个执行。

更新:Plunker 代码:https://embed.plnkr.co/mCJGo60sxQLQohWRYC3O/

编辑:要查看问题,您需要将一个按钮从一组移动到另一组,然后单击您移动的同一个按钮。你会看到绑定没有完成。

【问题讨论】:

  • 你能用 JSFiddle 演示这个问题吗?这会增加您获得(好的)答案的机会。
  • 您能否编辑您的帖子,以便我们可以看到未执行 ngOnInit 的组件,以及调用的组件
  • 当然。给我几分钟
  • 这里是 plunker 代码。 embed.plnkr.co/mCJGo60sxQLQohWRYC3O
  • 我刚刚检查过了。构造函数和 onInit 都在工作。问题出在哪里?

标签: angular angular5 sortablejs


【解决方案1】:

在路由时,如果您的路由器链接的参数仅发生变化,(在您的情况下为 :card.id)Angular 将不会销毁并重新创建您的组件。

只有当你的原始路由发生变化时,Angular 才会销毁并重新创建组件。

因此 onInit 没有被调用。

如果您的组件中需要 card.id,您可以像这样订阅参数:

constructor(route:ActivatedRoute) {
  route.params.subscribe(val => {
    console.log("Button ID: " + val.id);
  });
}

请注意,您需要在卡片编辑组件中导入并注入ActivatedRoute。

应用组件的完整代码:

//our root app component
import {Component, NgModule, VERSION, OnInit} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { SortablejsModule, SortablejsOptions } from 'angular-sortablejs';
import { Routes, RouterModule, ActivatedRoute } from '@angular/router';

@Component({
  selector: 'my-app',
  template: `<router-outlet></router-outlet>`,
})
export class App {

}

@Component({
  selector: 'my-cards',
  template: `
  <p> Try to move buttons </p>
   <div class="group" *ngFor="let group of groups" [sortablejs]="group.cards" [sortablejsOptions]="sortableOptions">
    <div class="card" *ngFor="let card of group.cards">
        <button routerLink="card/edit/{{card.id}}">Edit {{card.id}}</button>
    </div>
    <hr />
</div>
  `,
})
export class Cards {
  groups = [
    {
      id: 1,
      cards: [
        {
          id: 1,
        },
        {
          id: 2,
        },
        {
          id: 3,
        }
      ]
    },
    {
      id: 2,
      cards: [
        {
          id: 4,
        },
        {
          id: 5,
        },
        {
          id: 6,
        }
      ]
    }
  ];
  sortableOptions: SortablejsOptions = {
    group: 'checks'
  }
}

@Component({
  selector: 'my-card-edit',
  template: `
  <p routerLink="">Go Home</p>
  <p>{{content}}</p>
  <input type="text" value="{{content}}">
  `,
})
export class CardEdit implements OnInit {
  content = "content here";

  constructor(route: ActivatedRoute) {
    route.params.subscribe(val => {
     console.log("Button ID: " + val.id);
    });
  }



}

@NgModule({
  imports: [ 
    BrowserModule, 
    SortablejsModule.forRoot({
      animation: 0,
    }),
    RouterModule.forRoot([
      {path: '', component: Cards},
      {path: 'card/edit/:id', component: CardEdit}
    ])
  ],
  declarations: [ App, Cards, CardEdit ],
  bootstrap: [ App ]
})
export class AppModule {}

【讨论】:

  • 它适用于我没有移动的按钮。尝试移动一个按钮并单击它。这也是一个有用的评论
  • 谢谢。我看到它以某种方式工作。但是现在的主要问题是为什么它没有绑定?
  • @VinodBhavnani 你发现问题了吗?我没有使用路由更改,而是向组件提供输入,但组件没有检测到更改。因此不更新视图。不确定 ngOnInit 触发和构造函数有什么问题。仅当我单击该页面时才会触发。最终在单击 Input 或 ngOnChanges 后也会被解雇。如果您找到任何真正的解决方案,请告诉我
  • @Deepender ,你能用你的代码创建一个新问题吗?没有代码我无法弄清楚:)
  • 肯定会这样做
猜你喜欢
  • 2018-01-14
  • 1970-01-01
  • 2016-06-16
  • 1970-01-01
  • 2021-11-27
  • 2018-09-13
  • 2019-08-30
相关资源
最近更新 更多