本文整理汇总了TypeScript中@angular/router.RouterModule.forRoot方法的典型用法代码示例。如果您正苦于以下问题:TypeScript RouterModule.forRoot方法的具体用法?TypeScript RouterModule.forRoot怎么用?TypeScript RouterModule.forRoot使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类@angular/router.RouterModule
的用法示例。
在下文中一共展示了RouterModule.forRoot方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1:
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeContainer } from './home';
import { LoginContainer, LoginGuard } from './login';
const ROUTES: Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginContainer, canActivate: [ LoginGuard ] },
{ path: 'admin', loadChildren: () => System.import('./+admin/admin.module') }
];
export const ROUTING: ModuleWithProviders = RouterModule.forRoot(ROUTES, { useHash: false });
示例2:
import { RouterModule, Routes } from '@angular/router';
import { UserComponent } from "./user/user.component";
import { HomeComponent } from "./home-component.component";
import { USER_ROUTES } from "./user/user.routes";
import { DataDrivenComponent } from "./11_Forms/data-driven/data-driven.component";
import { TemplateDrivenComponent } from "./11_Forms/template-driven/template-driven.component";
const APP_ROUTES: Routes = [
{ path: 'user', redirectTo: '/user/1', pathMatch: 'full' },
{ path: 'user/:id', component: UserComponent },
{ path: 'user/:id', component: UserComponent, children: USER_ROUTES },
//{ path: '', component: HomeComponent }
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
//11 Forms
{ path: 'formsData', component: DataDrivenComponent },
{ path: 'formsTemplate', component: TemplateDrivenComponent },
{ path: '**', redirectTo: '/user/2', pathMatch: 'full' }
];
export const routing = RouterModule.forRoot(APP_ROUTES);
//export const APP_ROUTES_PROVIDER = [
// provideRoutes(APP_ROUTES)
//];
示例3:
import { Routes, RouterModule } from "@angular/router";
import { MessagesComponent } from "./messages/messages.component";
import { AuthenticationComponent } from "./auth/authentication.component";
const appRoutes: Routes = [
{
// Root path setup
path: '',
redirectTo: '/messages',
pathMatch: 'full'
},
{
path: 'messages',
component: MessagesComponent
},
{
path: 'auth',
component: AuthenticationComponent,
// Implementation of nested routes
// It's loaded only when needed
loadChildren: './auth/auth.module#AuthModule'
}
];
// Register all routes and export it
export const routing = RouterModule.forRoot(appRoutes);
示例4:
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule, PreloadAllModules } from '@angular/router';
import { AppComponent } from './app.component';
import { LandingComponent } from './landing/landing.component';
import { LogInComponent } from './login/login.component';
import { NgrxDashBoardComponent } from './ngrx/dashboard.component';
import { LogInGuard } from './shared/services/login.guard';
const routes: Routes = [
{ path: '', component: LandingComponent, canActivate: [LogInGuard] },
{ path: 'login', component: LogInComponent, canActivate: [LogInGuard] },
{ path: 'ngrx', component: NgrxDashBoardComponent, canActivate: [LogInGuard] },
{
path: '*',
redirectTo: '/',
pathMatch: 'full'
}
];
export const routing: ModuleWithProviders =
RouterModule.forRoot(routes, {
preloadingStrategy: PreloadAllModules
})
;
示例5:
import { AuthGuard } from './common/auth.guard';
import { HomeComponent } from './components/home/home.component';
import { LoginComponent } from './components/login/login.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';
import { ErrorComponent } from './components/error/error.component';
export const routing = RouterModule.forRoot([
{
path: '',
pathMatch: 'full',
component: HomeComponent
},
{
path: 'login',
component: LoginComponent
},
{
path: 'dashboard',
component: DashboardComponent,
canActivate: [AuthGuard]
},
{
path: 'error',
component: ErrorComponent
},
{
path: '**',
redirectTo: 'error'
}
]);
示例6:
import {DashboardComponent} from './dashboard/dashboard.component';
import {RouterModule} from '@angular/router';
import {SettingsComponent} from './settings/settings.component';
const routes = [
// {path: '', loadChildren: 'src/app/dashboard/dashboard.module'},
{path: '', component: DashboardComponent},
// {path: 'settings', loadChildren: 'app/settings/settings.module'},
{path: 'settings', component: SettingsComponent}
];
export default RouterModule.forRoot(routes);
示例7:
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HeroesComponent } from './heroes.component';
import { DashboardComponent } from './dashboard.component';
import { HeroDetailComponent } from './hero-detail.component';
const appRoutes: Routes = [
{
path: 'heroes',
component: HeroesComponent
},
{
path: 'dashboard',
component: DashboardComponent
},
{
path: '',
redirectTo: '/dashboard',
pathMatch: 'full'
},
{
path: 'detail/:id',
component: HeroDetailComponent
}
];
export const routing: ModuleWithProviders
= RouterModule.forRoot(appRoutes);
示例8:
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{
path: '',
data,
component: LayoutComponent,
children: [
{ path: 'components', loadChildren: './components/components.module#ComponentsModule' },
{ path: 'docs', loadChildren: './docs/docs.module#DocsModule' },
{ path: 'home', loadChildren: './home/home.module#HomeModule' },
{ path: 'interface', loadChildren: './interface/interface.module#InterfaceModule' },
{ path: 'profile', loadChildren: './profile/profile.module#ProfileModule' },
{ path: 'forms', loadChildren: './forms/forms.module#FormsModule' },
{ path: 'gallery', loadChildren: './gallery/gallery.module#GalleryModule' },
{ path: 'messages', loadChildren: './messages/messages.module#MessagesModule' },
{ path: 'empty', children: []},
],
},
{
path: '',
data,
component: LayoutBaseComponent,
children: [
{ path: '', loadChildren: './auth/auth.module#AuthModule' },
{ path: '**', redirectTo: '/404?message=Invalid route' },
],
},
]
export const AppRoutingModule: ModuleWithProviders = RouterModule.forRoot(routes)
示例9:
import { RouterModule } from "@angular/router";
import { ModuleWithProviders } from '@angular/core';
import { appRoutes } from './app.routes';
export const appRouting: ModuleWithProviders = RouterModule.forRoot(appRoutes, { useHash: true });
示例10:
import {Router, RouterModule} from '@angular/router';
import {HomeComponent} from './home.component';
import {MessagesComponent} from './messages/messages.component';
import {PhotosComponent} from './photos/photos.component';
import {NotFoundComponent} from './not-found.component';
export const routing = RouterModule.forRoot([
{path:'', component: HomeComponent},
{path:'messages', component: MessagesComponent},
{path:'photos', component: PhotosComponent},
{path:'**', component: NotFoundComponent},
]);