循环依赖中带有警告的角路由服务

时间:2018-11-09 19:29:12

标签: angular typescript routes circular-dependency

我正在尝试在我的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使用路由服务,因此我得到了

  

在循环依赖项中检测到警告

我如何解决它,还有更好的方法吗?

谢谢

1 个答案:

答案 0 :(得分:0)

我认为您可以对“命名”路线使用方法。创建单独的class Url,其中包含地图名称=>路径,并在export const routes: Routes = [...]数组和RouteService.getRouteByTerm方法中使用该Url类(并从RouteService类中删除this.routes)。

这里是link with details.

相关问题