简体   繁体   English

Angular 5-不执行构造函数和ngOnInit

[英]Angular 5 - Constructor and ngOnInit are not executed

I am using Angular 5 in combination with SortableJs. 我将Angular 5与SortableJs结合使用。

On a page I have listed multiple cards grouped. 在页面上,我列出了分组的多张卡片。

HTML looks like 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>

After I move a card from one group to another and I click on edit button it will redirect me to a page where I can edit the selected card. 将卡从一个组移动到另一个组并单击“编辑”按钮后,它将重定向到页面上,我可以在其中编辑所选卡。

When it redirects me to that page the constructor and ngOnInit are not executed. 当它将我重定向到该页面时,不会执行构造函数和ngOnInit。

After I click an element, those two execute. 单击元素后,将执行这两个。

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

EDIT: To see the problem you need to move one button from one group to another then click the same button you moved. 编辑:要查看问题,您需要将一个按钮从一组移动到另一组,然后单击您移动的相同按钮。 You will see that the binding are not done. 您将看到绑定未完成。

When routing, if only the params of your router link are changing, (In your case :card.id) Angular will not destroy and recreate your component. 路由时,如果仅更改路由器链接的参数,(在您的情况下:card.id)Angular不会销毁并重新创建组件。

Angular destroys and recreates component only if your original route changes. 仅当原始路线更改时,Angular才会销毁并重新创建组件。

Hence onInit is not being called. 因此,不会调用onInit。

If you need the card.id in your component, you can subscribe to the params like this: 如果您的组件中需要card.id,则可以订阅以下参数:

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

Note that you need to import and inject ActivatedRoute in your card edit component. 请注意,您需要在卡编辑组件中导入和注入ActivatedRoute。

Entire code for app-component: 应用程序组件的完整代码:

//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 {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM