routerLink not compiling with TS template syntax

半城伤御伤魂 提交于 2019-12-12 05:17:37

问题


I have a component like this 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")}`;
  }
}

Any ideas why it's not compiling link value or how to make it work? The goal is to hardcode url, and just change text of the generated link.

UPDATE:

I also tried:

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

which works in plunker with #/two because of HashLocationStrategy, but in my app router isn't detecting the link and whole page reloads...


回答1:


routerLink is a directive. Directives and components are not created for HTML added with innerHTML.

If you make link an Array instead of a string.

This should work

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


来源:https://stackoverflow.com/questions/35685342/routerlink-not-compiling-with-ts-template-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!