如何在Ember.js中创建条件链接?

Jef*_*y C 0 conditional link-to ember.js

我使用的是Ember.js版本2.8.2.

我想在link-to条件为真的情况下将内容包装在里面.

首先尝试:

{{#if isAdmin}}
  {{#link-to admin}}
    contents here
  {{/link-to}}
  {{else}}
    contents here
{{/if}}
Run Code Online (Sandbox Code Playgroud)

问题:代码不干,因为内容重复两次.

我该怎么做?谢谢.

yka*_*gol 5

第一种选择:

如果要从dom中删除它,请将"link-to"组件包装为组件(my-admin-link.hbs):

{{#if isAdmin}}
  {{#link-to admin}}
    {{yield}}
  {{/link-to}}
{{else}}
    {{yield}}
{{/if}}
Run Code Online (Sandbox Code Playgroud)

比使用它:

{{#my-admin-link}}
   your content
{{/my-admin-link}}
Run Code Online (Sandbox Code Playgroud)

第二种选择:

使用disabled-disabledClass of link-to:

{{#link-to admin disabled=isNotAdmin disabledClass='showastext'}}
    your content
{{/link-to}}
Run Code Online (Sandbox Code Playgroud)

在你的app.css中,showastext可以定义为:

.showastext{
    text-decoration: none;
    cursor: text;
    color: black;
}
Run Code Online (Sandbox Code Playgroud)