等效于Angular的APP_INITIALIZER的模块

时间:2018-07-05 12:22:47

标签: angular

在Angular中,功能模块的the APP_INITIALIZER等效项是什么?当我的功能模块(延迟)加载时,我想运行一些代码。该代码将需要注入一些依赖项。

虽然我似乎找不到任何类似的模块项目。我曾在Angular docs网站(“初始化程序”)上使用过搜索,但一无所获。我浏览了模块文档的各个部分,并在浏览器中使用了类似的搜索词。但是我什么都没找到。

如何在Angular中的模块加载上运行代码(取决于通过DI的共享服务)?


PS。可能这是一个XY问题,其中“ Y”是我上面的问题。此时,我对学习“ Y”本来就很感兴趣,但是为了完整起见,我的“ X”是我希望功能模块回调共享模块中的 public class MultiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private ArrayList<MYDataProvider> arrayList; private final static int VIEW0 = 0; private final static int VIEW1 = 1; public MultiAdapter(ArrayList<MyDataProvider> _arrayList) { this.arrayList = new ArrayList<>(); this.arrayList = _arrayList; } @Override public int getItemViewType(int position) { switch (position) { case 0: return VIEW0; case 1: default: return VIEW1; }//switch }//get item view type @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { int layoutRes; View view; RecyclerView.ViewHolder viewholder; switch (viewType) { case VIEW0: layoutRes = R.layout.card_item; view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutRes, viewGroup, false); viewholder = new E1(view); break; case VIEW1: default: layoutRes = R.layout.item_layout; view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutRes, viewGroup, false); viewholder = new E2(view); break; }//switch return viewholder; }//on create @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { switch (holder.getItemViewType()) { case VIEW0: { MyDataProvider mydata = arrayList.get(position); ((E1)holder).name1.setText(mydata.getD_name() + "this is item " + position); ((E1)holder).imageView1.setImageResource(arrayList.get(position).getImg_res()); } break; case VIEW1: { MyDataProvider mydata = arrayList.get(position); ((E2)holder).name2.setText(mydata.getD_name() + "this is item " + position); ((E2)holder).imageView2.setImageResource(arrayList.get(position).getImg_res()); } break; }//switch }//on bind @Override public int getItemCount() { return arrayList.size(); } public class E1 extends mainViewHolder { ImageView imageView1; TextView name1; public E1(View itemView) { super(itemView); imageView1 = itemView.findViewById(R.id.image); name1 = itemView.findViewById(R.id.d_name); } }//e1 class public class E2 extends mainViewHolder { ImageView imageView2; TextView name2; public E2(View itemView) { super(itemView); imageView2 = itemView.findViewById(R.id.person_photo); name2 = itemView.findViewById(R.id.person_name); } }//e2 class public class mainViewHolder extends RecyclerView.ViewHolder { public mainViewHolder(@NonNull View itemView) { super(itemView); } } }//adapter 以注册所有功能模块要做出贡献。

1 个答案:

答案 0 :(得分:1)

不确定它是否是惯用的,但是我会建议使用模块constructor来解决这个问题。例如:

@NgModule({
  imports: [
    CommonModule,
    ThingsRoutingModule,
  ],
  declarations: [
    // Feature module components here
  ],
})
export class ThingsModule {
  constructor(myService: MySharedService) {
    myService.doSomething('From lazy loaded feature module!');
  }
}

MySharedService被注入的地方。