angular路由

路由跳轉:javascript

  angular2   java

navigate是Router類的一個方法,主要用來跳轉路由。瀏覽器

1.this.router.navigate(['user', 1]);
以根路由爲起點跳轉angular2

2.this.router.navigate(['user', 1],{relativeTo: route});
默認值爲根路由,設置後相對當前路由跳轉,route是ActivatedRoute的實例,使用須要導入ActivatedRouteantd

3.this.router.navigate(['user', 1],{ queryParams: { id: 1 } });
路由中傳參數 /user/1?id=1app

4.this.router.navigate(['view', 1], { preserveQueryParams: true });
默認值爲false,設爲true,保留以前路由中的查詢參數/user?id=1 to /view?id=1this

5.this.router.navigate(['user', 1],{ fragment: 'top' });
路由中錨點跳轉 /user/1#top url

6.this.router.navigate(['/view'], { preserveFragment: true });
默認值爲false,設爲true,保留以前路由中的錨點/user/1#top to /view#topcode

7.this.router.navigate(['/user',1], { skipLocationChange: true });
默認值爲false,設爲true路由跳轉時瀏覽器中的url會保持不變,可是傳入的參數依然有效component

8.this.router.navigate(['/user',1], { replaceUrl: true });
未設置時默認爲true,設置爲false路由不會進行跳轉

 

路由示例

 

app.module.ts

export const ROUTES: Routes = [
  { path: '', redirectTo: '/', pathMatch: 'full' },
  { path: 'index',component: ZyComponent },
  { path: 'home',loadChildren: './app-routing.module#AppRoutingModule'},
  { path: 'login', component: LoginComponent },
  { path: 'register', component: RegisterComponent },
  { path: 'forgetPWD', component: ForgetPasswordComponent }
];

imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    FormsModule,
    RouterModule.forRoot(ROUTES, { useHash: true }),
    ReactiveFormsModule,
    BrowserAnimationsModule,
    /** 導入 ng-zorro-antd 模塊 **/
    NgZorroAntdModule,
    FileUploadModule,
  ],

  app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { EditPasswordComponent } from './edit-password/edit-password.component';
import { NoticeComponent } from './notice/notice.component';
import { NoticeDetailComponent } from './notice-detail/notice-detail.component';
import { PositionComponent } from './position/position.component';
import { Step1Component } from './step1/step1.component';
import { Step2Component } from './step2/step2.component';
import { Step3Component } from './step3/step3.component';
import { OkComponent } from './ok/ok.component';
import { InformationComponent } from './information/information.component';
import { InformationDetailComponent } from './information-detail/information-detail.component';
import { PersonalComponent } from './personal/personal.component';
import { MyApplyComponent } from './my-apply/my-apply.component';
import { ReportComponent } from './report/report.component';
import { HomeComponent } from './home/home.component';
import { FormComponent } from './form/form.component';

const routes: Routes = [];
export const ROUTES: Routes = [
  {
    path: '',
    component: HomeComponent,
    children: [
      { path: '', redirectTo: '/index', pathMatch: 'full' },
      {
        path: 'position',
        children: [
          { path: 'form', component: FormComponent },
          { path: 'step1', component: Step1Component },
          { path: 'step2', component: Step2Component },
          { path: 'step3', component: Step3Component },
          { path: 'ok', component: OkComponent },
          { path: '**', component: PositionComponent }
        ]
      },
      {
        path: 'notice',
        children: [
          { path: 'noticeDetail', component: NoticeDetailComponent },
          { path: '**', component: NoticeComponent }
        ]
      },
      {
        path: 'personal',
        children: [
          { path: 'myApply', component: MyApplyComponent },
          { path: 'report', component: ReportComponent },
          { path: 'information',
          	children: [
		          { path: 'informationDetail', component: InformationDetailComponent },
		          { path: '**', component: InformationComponent }
		        ]
          },
          { path: 'editPassword', component: EditPasswordComponent },
          { path: '**', component: PersonalComponent }
        ]
      },
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(ROUTES)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
相關文章
相關標籤/搜索