Although @JS_astronauts already provided a solution, I think it would be instructive to explain the error...
The primary outlet is set when Angular parses an HTML template and finds directive RouterOutlet, i.e., when it finds RouterOutlet's selector: <router-outlet></router-outlet>
.
Although your template does contain <router-outlet></router-outlet>
, your component does not contain directives: [ROUTER_DIRECTIVES]
, so Angular won't look for any of the directives defined by that constant:
export const ROUTER_DIRECTIVES = [RouterOutlet, RouterLink, RouterLinkWithHref,
RouterLinkActive];
So when Angular parses AppComponent's HTML templates, if it doesn't recognize <router-outlet></router-outlet>
so it just ignores it. Later, when Angular tries to render the default route component (HomePage), it complains because doesn't know where to put it.
This error can also happen if you have a typo in your element selector, e.g.:
<roter-outlet></roter-outlet>
In this case, even if your directives
array is set correctly, Angular will never find the required <router-outlet>
element.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…