Create Generic Component With T in Constructor

前端 未结 1 410
长发绾君心
长发绾君心 2021-01-13 16:37

I\'m trying to make a generic component to display list resources. I\'m running into issue instantiating the component in HTML. I was using this answer to attempt to fix th

1条回答
  •  终归单人心
    2021-01-13 17:19

    I solved this by delaying the initialization, and making the dependency a @Input property

    export class PageListBaseComponent {
    
        @Input() detailPageType: any;
        @Input() set baseProvider(provider: ProviderBase) {
            provider.getList().subscribe(entities =>  {
                this.entityList = entities;
                console.log(entities);
            });
        }
    
        public entityList: T[];
    
        constructor(public navCtrl: NavController, public navParams: NavParams) 
        {
    
        }
    
        navigateToDetail(id: number) {
            console.log('test ' + id)
            this.navCtrl.push(this.detailPageType, { id })
        }
    }
    

    Then I Modify the Page to take in a concrete implementation of our dependency,

    export class UsersPage {
    
      public userDetailType: any = ProfilePage;
    
      constructor(public navCtrl: NavController, public navParams: NavParams, public baseProvider: Users) {
          console.log(this.userType);
      }
    } 
    

    Then I implement the component like so:

    
      
    
    

    0 讨论(0)
提交回复
热议问题