douboshan1466 2016-10-25 12:30
浏览 46

如何匹配APP MODULE TS中的任何路线与angular2 rc5?

I have a simple angular application and I am using Angular2 for the first time.

I am prompted with error in firefox console "EXCEPTION: Uncaught (in promise): Error: Cannot match any routes: 'books/huckleberry'"

book.component.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-directory',
  templateUrl: './book.component.html',
  styleUrls: ['./book.component.css']
})
export class BookComponent implements OnInit {

  book: string; 

  constructor(private route: ActivatedRoute) { 

    this.book= route.snapshot.params['book'];

  }

  ngOnInit() {
  }

}

app.routes.ts

import { Routes, RouterModule } from "@angular/router";
import { BookComponent } from "./book/book.component";
import { HomeComponent } from "./home/home.component";

const APP_ROUTES: Routes = [


    {
      path:'book',
      component: BookComponent,
      children: [

        { path: 'book/:book', component: BookComponent}

      ]
    },
    { path: '', component: HomeComponent }

];

export const APP_ROUTES_PROVIDER = RouterModule.forRoot(APP_ROUTES);

app.module.ts

...
import { BookComponent } from './book/book.component';

import { RouterModule } from "@angular/router";

@NgModule({
  declarations: [
    AppComponent,
    BookComponent,
    HomeComponent,
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule.forRoot([
      { path: '', component: HomeComponent },
      { path: 'book', component: BookComponent },
      // { path: '**', component: PageNotFoundComponent }
    ]),

  RouterModule.forChild([
    {
      path: 'book', //parent path
      children: [
        {
          path: 'book/:book', 
          component: BookComponent,
        }
      ]
    }
  ])

  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

When i access http://localhost:4200/book, there is no error. But when i access http://localhost:4200/book/huckleberry, using Angular2 for the first time.

I am prompted with error in firefox console "EXCEPTION: Uncaught (in promise): Error: Cannot match any routes: 'book/huckleberry'"

Do you know how can i update app.module.ts?

I should be able to get {{book}} in book.component.html

  • 写回答

1条回答 默认 最新

  • douwan4993 2016-10-25 14:34
    关注

    I was able to solve this by the following codes below:

    app.routes.ts const APP_ROUTES: Routes = [

        { path:'directory', component: BookComponent },
        { path:'directory/:book', component: BookComponent },
        { path: '', component: HomeComponent }
    
    ];
    

    app.module.ts

    @NgModule({
      declarations: [
        AppComponent,
        DirectoryComponent,
        HomeComponent,
      ],
      imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        RouterModule.forRoot([
          { path: '', component: HomeComponent },
          { path: 'book', component: BookComponent },
          { path: 'book/:book', component: BookComponent }
          // { path: '**', component: PageNotFoundComponent }
        ])
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    

    With the code above, i am now able to get the parameter and print it to the page via {{book}}

    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面