【问题标题】:routerLink not compiling with TS template syntaxrouterLink 未使用 TS 模板语法编译
【发布时间】:2016-02-28 16:48:22
【问题描述】:

我有这样的组件http://plnkr.co/edit/mJDHZIm5IzeMB9f2IVLE:

@Component({
  template: `
      <p [innerHTML]="link"></p>
  `,
  directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
  { path: '/two', component: Two, name: 'Two'},
])
export class App {
  two = (text: string) => `<a [routerLink]="['/Two']">${text}</a>`;
  constructor(private _router: Router) {  }
  ngOnInit() {
    this.link = `${this.two("two")}`;
  }
}

任何想法为什么它不编译 link 值或如何使其工作?目标是对 url 进行硬编码,并且只更改生成的链接的文本。

更新:

我也试过了:

two = (text: string) => `<a href="/two">${text}</a>`;

由于 HashLocationStrategy,它与 #/two 一起工作,但在我的应用程序中,路由器没有检测到链接并且整个页面重新加载...

【问题讨论】:

    标签: typescript angular angular2-template


    【解决方案1】:

    routerLink 是一个指令。不会为使用innerHTML 添加的 HTML 创建指令和组件。

    如果您将link 设为Array 而不是string

    这应该可以工作

    <p [routerLink]="link"></p>
    
    this.link = ['Two'];
    

    【讨论】:

    • 我正在尝试对链接/hef 进行硬编码,并且只是更改文本值/标签。
    • 为什么不直接绑定标签文本和href而不是整个链接呢?如果您希望它动态添加,您需要使用DynamicComponentLoader
    • 因为它需要进入另一个组件(通用通知),我只是简化了很多例子(;
    • 目前选项非常有限。要么你没有得到任何绑定或指令,要么你使用 DCL。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 2014-05-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多