我无法在AppRoutingModule中使用TranslateService吗?

时间:2019-04-01 14:51:20

标签: angular angular5

因此,我正在使用自动面包屑模块​​,我需要使用我的TranslateService翻译标题,声明路线的const时使用它的方式是什么。

这是我的AppRoutingModule

import { NgModule, Injector } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

// Components ========================================
import { HomeComponent } from './pages/home/home.component';
import { AltaRepresentacioComponent } from './pages/representacio/alta-representacio/alta-representacio.component';
import { ConfirmacioAltaComponent } from './pages/representacio/alta-representacio/confirmacio-alta.component';
import { ConsultaRepresentacioComponent } from './pages/representacio/consulta-representacio/consulta-representacio.component';
import { ConsultaAplicacioComponent } from './pages/aplicacio/consulta-aplicacio/consulta-aplicacio.component';
import { PermisosComponent } from "./pages/permisos/permisos.component";
import { ErrorPageComponent } from "./shared/error/error-page.component";
import { TranslateService} from '@ngx-translate/core';

const routes: Routes = [
  { path: '*', redirectTo: '' },
  {
    path: '',
    component: HomeComponent,
  },
  {
    path: 'representacio',
    children: [
      {
        path: 'alta',
        children: [
          {
            path: '',
            component: AltaRepresentacioComponent
          },
          {
            path: 'confirmacio',
            component: ConfirmacioAltaComponent,
            data: {
              breadcrumb: 'Confirmació de Representació'
            }
          }
        ],
        data: {
          breadcrumb: 'Alta de Representació'
        }
      },
      {
        path: 'consulta',
        component: ConsultaRepresentacioComponent,
        data: {
          breadcrumb: 'Consulta Representacions'
        }
      }
    ]
  },
  {
    path: 'permisos',
    component: PermisosComponent,
    data: {
      breadcrumb: 'Permisos'
    }

  },
  {
    path: 'error',
    component: ErrorPageComponent,
    data: {
      breadcrumb: 'ErrorPage'
    }
  }
];

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

我在这里使用面包屑(HTML):

<div class="col-12 no-left-padd">
            <nav aria-label="breadcrumb">
              <ol class="breadcrumb">
                <li *ngFor="let breadcrumb of breadcrumbs; let last = last;" class="breadcrumb-item">
                  <span *ngIf="!last">
                    <a routerLink="{{breadcrumb.url}}">
                      {{ breadcrumb.label }}
                    </a>
                  </span>
                  <span *ngIf="last"> {{ breadcrumb.label }} </span>
                </li>
                <!-- <li class="breadcrumb-item active" aria-current="page">Consulta representació</li> -->
              </ol>
            </nav>
          </div>

我用ActivatedRoute更改面包屑的代码段:

ngOnInit() {
        let breadcrumb: BreadCrumb = {
            label: this.translate.instant('breadcrumb.inici'),
            url: ''
        };

        this.router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe(event => {
            //set breadcrumbs
            let root: ActivatedRoute = this.route.root;
            this.breadcrumbs = this.getBreadcrumbs(root);
            this.breadcrumbs = [breadcrumb, ...this.breadcrumbs];

        });
    }

我尝试了几种方法,但是我对angular和Typescript真的很陌生,所以我不太了解Inject服务之类的功能如何工作,所以如果您能帮助我找到一种方法,解决这个问题。因为我知道使用TranslateService的唯一方法是在构造函数中声明它,但随后我无法使用它创建路线const。

1 个答案:

答案 0 :(得分:0)

您可以将i18n密钥提供给面包屑:

data: {
  breadcrumb: 'foo.bar.xyz'
}

然后,您可以在模板中使用TranslatePipe中的@ngx/translate

<a routerLink="{{breadcrumb.url}}">
  {{ breadcrumb.i18nKey | translate }}
</a>