如果在使用组件时出现以下错误:
ERROR Error: Uncaught (in promise): Error: Template parse errors: 'xxx-component' is not a known element
则需要将该组件引入到模块中,例如:
import { XxxComponent } from './xxx.component';
@NgModule({ declarations: [ XxxComponent, ... ], ... })
在NgModule中,如果把同一个组件声明了两次,会导致以下错误:
Error: Type XxxComponent is part of the declarations of 2 modules: Module1 and Module2!
需要把组件从其中一个模块中删除即可。
在使用路由守卫时,如果使用了某个组件,则需要确保该组件已经被加载。否则可能会导致以下错误:
Error: Cannot activate an already activated outlet
可以在路由守卫中使用resolve来确保组件已经加载完成,例如:
const routes: Routes = [ { path: 'xxx', component: XxxComponent, resolve: { data: ComponentResolver } } ];
@Injectable()
export class ComponentResolver implements Resolve
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { return this.xxxService.getSomeData() .pipe( catchError(error => of(error)) ); } }