首页 文章

Angular 2子路由(v3)'Cannot read property ' annotations ' of undefined'

提问于
浏览
4

我正试图在Angular 2中玩游戏并运行测试应用程序,但是我在使用最新版本的路由器(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 {

}

出于某种原因,我可以启动应用程序,但尝试单击配置文件routerLink会导致错误:

“EXCEPTION:错误:未捕获(在承诺中):TypeError:无法读取未定义的属性”注释“

如果有人能帮我解决这个问题,我将不胜感激 .

2 回答

  • 1

    听起来好像:

    类似问题:

    检查常见错误:

    • 确保您的路线 path 中没有 /

    • 确保每条路线都有 componentredirectTochildren

    • 确保将路由中使用的组件正确导入到定义路由的文件中

  • 1

    我有这个错误使用webpack并尝试异步加载路由,如下所示:

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

    上面的代码没有错,但看看我认为我正在加载的模块:

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

    注意模块的名称,LockedModule,它应该是PasswordModule . 我偶然发现了这一点,我花了一段时间才发现 . 我不经常滚动到底部并验证导出名称和调试窗口没有给出任何迹象表明这是原因 .

相关问题