Angular 2 Child Routing (v3) 'Cannot read prop

2019-06-28 03:56发布

I'm trying to play around in Angular 2 and get a test app running but I'm having some problems getting the routing to work in the latest version of the router (3.0.0 alpha-7).

main.ts:

import {bootstrap}    from '@angular/platform-browser-dynamic';
import {AppComponent} from './app.component';
import {APP_ROUTER_PROVIDERS} from './app.routes';

bootstrap(AppComponent, [APP_ROUTER_PROVIDERS]);

app.component.ts:

import {Component} from '@angular/core';
import {ROUTER_DIRECTIVES} from '@angular/router';

@Component({
    selector: 'my-app',
    template: `
       <router-outlet></router-outlet>
    `,
    directives: [ROUTER_DIRECTIVES]
})
export class AppComponent { }

app.routes.ts:

import {provideRouter, RouterConfig} from '@angular/router';
import {SigninRoutes} from './signin/signin.routes';

export const AppRoutes: RouterConfig = [
    ...SigninRoutes
]

export const APP_ROUTER_PROVIDERS = [
    provideRouter(AppRoutes)
];

signin.component.ts:

import {Component} from '@angular/core';
import {ROUTER_DIRECTIVES} from '@angular/router';

@Component({
    selector: 'signin',
    template: `
        <a [routerLink]="['/signin']">Sign In</a>
        <a [routerLink]="['/profile']">Profile</a>
        <br><br>Sign In Test
        <router-outlet></router-outlet>
    `,
    directives: [ROUTER_DIRECTIVES]
})

export class SigninComponent {

}

signin.routes.ts:

import {Component} from '@angular/core';
import {ROUTER_DIRECTIVES} from '@angular/router';

@Component({
    selector: 'signin',
    template: `
        <a [routerLink]="['/signin']">Sign In</a>
        <a [routerLink]="['/profile']">Profile</a>
        <br><br>Sign In Test
        <router-outlet></router-outlet>
    `,
    directives: [ROUTER_DIRECTIVES]
})

export class SigninComponent {

}

profile.component.ts:

import {Component} from '@angular/core';

@Component ({
    selector: 'profile',
    template: `
        Profile Test
    `

})

export class ProfileComponent {

}

For some reason, I can initiate the app alright, but attempting to click the Profile routerLink results in the error:

"EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot read property 'annotations' of undefined"

If anyone could help me out with this, it'd be much appreciated.

2条回答
相关推荐>>
2楼-- · 2019-06-28 04:32

Sounds like:

Similar issues:

Check for common errors:

  • Ensure you don't have / in path of your routes
  • Ensure every route has one of component, redirectTo, children
  • Ensure the components used in the routes are properly imported to the files where the routes are defined
查看更多
劳资没心,怎么记你
3楼-- · 2019-06-28 04:46

I had this error using webpack and trying to asynchronously load routes like so:

{
 path: 'password', loadChildren: () => new Promise(resolve => {
   (require as any).ensure([], require => {
     resolve(require('./password/password.module').PasswordModule);
   });
 })
},

Nothing wrong with the above code but then look at the module I thought I was loading:

@NgModule({
imports: [
 CommonModule,
 ...
],
declarations: [
  ...
],
providers: [
 ...
]
})
export class LockedModule {}  // wrong name !!!!

Notice the name of the module, LockedModule, it should be PasswordModule. I stumbled onto this and it took me a while to find. It's not very often I scroll to the bottom and verify the export name and the debug window gave me no indication that this was the cause.

查看更多
登录 后发表回答