ionic4之自定义component的使用事项

自定义了一个component, 结果在其他页面引用时报了错:

'xxx' is not a known element.

之前使用自定义的component倒是正常的。排查了代码,发现是非懒加载页面的方式引起的。
以前通过lazyload来加载一个页面时,一般这样:

 { path: 'test', loadChildren: '../x/xx/xxx.module#XXXPageModule' }
// 然后在XXXPageModule中import对应的自定义component或者components.module.ts
// 最后在xxx.page.html中直接可以使用

但现在是非懒加载方式,如:

 { path: 'test2', component: XXXPage }

那就需要在定义import XXXPage之前就把component给引用进来,比如:

@NgModule({
  imports: [
    ComponentsModule,  // ComponentsModule来自components.module.ts,包含了你自定义的component集合
    RouterModule.forChild([
       { path: '', component: HomePage },
       { path: 'test2', component: XXXPage } // XXXPage使用了你自定义的component
    ])
  ],
  declarations: [XXXPage],
  providers: [],
})
export class HomePageModule {
}
发布了372 篇原创文章 · 获赞 1031 · 访问量 242万+

猜你喜欢

转载自blog.csdn.net/moxiaomomo/article/details/87089235