首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角7 url路由配置

角7 url路由配置
EN

Stack Overflow用户
提问于 2019-01-04 07:18:57
回答 1查看 1.4K关注 0票数 1

我正在处理一个Range7应用程序,我现在正面临一个有趣的问题。

我的目标是在整个应用程序中不使用“硬编码URL”。

因此,我有一种方法,比如维护一个配置文件,其中我可以拥有所有的站点URL,并提供所需的组件和模块。

具有类似于以下代码的配置。

routes.ts // Url配置文件

代码语言:javascript
复制
import { LayoutComponent } from './layout/layout.component';
import { AdminIndexComponent } from './admin-index/admin-index.component';
import { AdminRegisterComponent } from './admin-register/admin-register.component';
import { LoginComponent } from './login.component';

export class AppRoutes {

    // Angular components url
    public static componentsUrl = {
        base: {
            path: '',
            component: AdminIndexComponent,
            pathMatch: 'full'
        },

        register: {
            path: 'register',
            component: AdminRegisterComponent
        },

        login: {
            path: 'login',
            component: LoginComponent
        },

        home: {
            path: 'home',
            component: LayoutComponent
        }
    };

routes-service.ts

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { AppRoutes } from './routes';
import { environment } from '../environments/environment';

@Injectable({
  providedIn: 'root',
})

export class AppRouterService {
  routesArray = [];

  getAppRoutesArray() {
    Object.entries(AppRoutes.componentsUrl).forEach(
        ([key, value]) => this.routesArray.push(value)
    );
    return this.routesArray;
  }
}

app-routing.module

代码语言:javascript
复制
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AppRouterService } from './routes-service';

const routes: Routes = ;

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

在这个层次上一切都很好。我可以提供路由的URL。

但有趣的是,我还有另一个组件需要URL。

admin-index.component.ts

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { RegistrationModule } from 'shared-components';
import { AppRouterService } from '../routes-service'; // Causing the circular dependency

@Component({
  selector: 'app-admin-index',
  templateUrl: './admin-index.component.html',
  styleUrls: ['./admin-index.component.scss']
})
export class AdminIndexComponent implements OnInit {

  appRoutesUrl;
  constructor(private regModule: RegistrationModule, private routerService: AppRouterService) { }

  ngOnInit() {
    this.appRoutesUrl = this.routerService.getAppRoutesObject(); // This variable in binding to component html. So I can retrieve the url from config file
  }
}

当我这样做的时候,我收到了一个类似于“检测到循环依赖项中的警告”的警告。我知道"AdminIndexComponent“在routes.ts上的导入导致了这一警告。

我能知道如何处理这个问题吗?另外,请提出一个有效的方法来做这件事?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2019-03-12 04:00:06

app.module.ts中的第一个,添加

代码语言:javascript
复制
import { RouterModule, Routes} from '@angular/router';`

然后加上这个进口,

代码语言:javascript
复制
RouterModule.forRoot(appRoutes)

之后,您可以在同一个文件中创建如下所示的路由。

代码语言:javascript
复制
    const appRoutes: Routes = [
      {path: '', component:AdminIndexComponent},
      {path: 'register', component:AdminRegisterComponent},
      {path: 'login', component:LoginComponent},
      {path: 'home', component:LayoutComponent}
    ]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54034590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档