新路由器(3.0.0)提供provideRouter
函数,该函数采用类型为RouterConfig
的数组参数...
官方开发人员指南通过引导const
提供教程。例如:
export const APP_ROUTER_PROVIDERS = [
provideRouter(routes)
];
我只想将其作为class
-->
client.ts
引导程序文件
...
import {APP_ROUTER} from './app/app.routes';
...
bootstrap(AppComponent, [HTTP_PROVIDERS, APP_ROUTER, ...]);
-->
app.route.ts
路由文件
import {Injectable} from '@angular/core';
import { provideRouter, RouterConfig } from '@angular/router';
import { somecomponents };
@Injectable()
export class APP_ROUTER {
constructor(
RouterConfig: RouterConfig
) {
this.ROUTE_prov = [provideRouter(this.prov)];
}
public ROUTE_prov;
public prov : RouterConfig = [
{ path: '/', component: HomePageComponent },
{ path: 'home', component: HomePageComponent },
{ path: 'about', component: AboutPageComponent },
{ path: 'landing', component: LandingPageComponent },
{ path: 'pp/:nickName', component: PersonalPageComponent },
{ path: 'carlos', component: CarlosPageComponent },
{ path: '**', redirectTo: 'home' }
];
}
答案 0 :(得分:0)
router-config.ts
import {provideRouter, RouterConfig} from '@angular/router';
export const APP_ROUTES : RouterConfig = [
{path: '', component: AppComponent},
// more paths
];
export const APP_ROUTE_PROVIDERS = [
provideRouter(APP_ROUTES)
];
main.ts
bootstrap(AppComponent, [HTTP_PROVIDERS, APP_ROUTER_PROVIDERS, ...]);
答案 1 :(得分:0)
发表评论后请尝试:
import {Injectable} from '@angular/core';
import { provideRouter, RouterConfig } from '@angular/router';
import { somecomponents };
@Injectable()
export class APP_ROUTER {
constructor(
) {
this.ROUTE_prov = [provideRouter(this.prov)];
}
public ROUTE_prov;
public prov : RouterConfig = [
{ path: '/', component: HomePageComponent },
{ path: 'home', component: HomePageComponent },
{ path: 'about', component: AboutPageComponent },
{ path: 'landing', component: LandingPageComponent },
{ path: 'pp/:nickName', component: PersonalPageComponent },
{ path: 'carlos', component: CarlosPageComponent },
{ path: '**', redirectTo: 'home' }
];
}
然后在你的main.ts中:
bootstrap(AppComponent, [HTTP_PROVIDERS, RouterConfig, APP_ROUTER, ...]);