首页 > 解决方案 > Angular CLI / TypeScript方法中的RouterLink?

问题描述

我必须将我的 URL 链接 (actionsConfig) 导入我的组件。

在 HTML 中我曾经使用“路由器链接”,它在 TypeScript 中是如何工作的?

动作配置.ts:

export const ACTIONS = [
  {
    label: 'Delete',
    actionType: 'DELETE',
  },
  {
    label: 'Edit',
    actionType: 'GO_TO',
    getUrl: row => '/detail/' + row.id,
  },
];

组件.ts:

actionFunc(action, user: User) {
if (action.actionType === 'DELETE') {
  if (confirm('Are you sure you want to delete this item?') === true) {
    /*delete ok*/
  }
}
if (action === 'GO_TO') {
  const url = action.getUrl(user);
  /* GO TO URL (routerlink?) */
}

}

标签: htmlangulartypescript

解决方案


更新 这个对我有用。

constructor(private router: Router) {
  }


  /.../
  if (action.actionType === 'GO_TO') {
      console.log('test');
      const url = action.getUrl(user);
      this.router.navigateByUrl(url).then();
    }

推荐阅读