簡體   English   中英

Angular 2在子組件中為[routerLink]模板化的正確方法

[英]Angular 2 proper way to template a [routerLink] in a child component

我正在嘗試找到在子組件中模板化[routerLink]的方法。

在我的app.component我有<router-outlet></router-outlet>

現在,在otherModule.component內部,我嘗試應用<a [routerLink]="['home']">Home</a>

app.module:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import {HomeModule} from "../home/home.module";
import {OtherModule} from "../otherModule/otherModule.module";
import {RouterModule} from "@angular/router";
import {routes} from "./routes";

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HomeModule,
    HttpModule,
    OtherModule,
    RouterModule.forRoot(routes)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

路線:

import {HomeComponent} from "../login/login.component";
import {OtherComponent} from "../clickStudio/clickStudio.component";

export const routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', component: HomeComponent },
  { path: 'other', component: OtherComponent },

]

其他組件:

import {Component} from "@angular/core";

@Component({
  selector: 'other',
  template: `<a [routerLink]="['home']">Home</a>`,
  styleUrls: ['./clickstudio-menu.component.scss']
})
export class OtherComponent {

}

我的主要問題是我需要路由路由器,而不是兒童路由

  • 有什么最佳實踐來實現這一目標?
  • 有什么辦法可以保持對routerLink的綁定?

你有沒有嘗試過

<a routerLink="home">Details</a>

要么

<a routerLink="/home">Details</a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM