【发布时间】:2018-11-09 19:29:11
【问题描述】:
我正在尝试在我的 Angular 6 应用程序中编写一个路由服务,任何组件都可以使用它来搜索我的应用程序中的路由。
结构如下图
home.component.ts
route.service.ts
routes.ts
app.module.ts
//routes.ts
import { HomeComponent } from './home.component.ts'
export const routes: Routes = [
{ path: 'home', component: HomeComponent }
{ path: '', component: OtherComponent }
]
//route.service.ts
import { routes } from './routes'
@Injectable({
providedIn: 'root'
})
export class RouteService {
....
getRouteByTerm(term: string) {
this.routes.filter( r => {r.name.includes(term)});
}
}
//home.component.ts
//Uses RouteService
import { RouteService } from './route.service'
...
constructor(public routeSearchService: RouteSearchService) {
this.routeSearchService.getRouteByTerm('home');
}
...
所以由于路由服务导入路由常量,路由导入 HomeComponent,现在我的 HomeComponents 使用路由服务,我得到了一个
检测到循环依赖中的警告
我该如何解决?有没有更好的方法?
谢谢
【问题讨论】:
标签: angular typescript routes circular-dependency