How to use string variables in ember helpers (linkTo or partial)?

前端 未结 4 648
攒了一身酷
攒了一身酷 2021-01-05 10:15

I need to setup array of menu links in the router and then render them in template using #each. But seems like #linkTo helper didn\'t recognize variables. How can i solve th

4条回答
  •  滥情空心
    2021-01-05 10:22

    This thread is not so recent and I do not know if most recent versions of Ember presents a different solution for this issue but on the Ember version 1.11 it works fine.

    The solution is the inline version of link-to.

    It works just like the helper (yes, it will toggle the active class for you depending on your current route) and allows you to pass dynamic parameters. Simulating the author's situation, we would have something like:

    component.js

    mainControls: [
      { path: 'widgets.new' },
      { name: 'Add' },
      { classes: 'btn btn-success icon-ok-sign' }
    ]
    

    component.hbs

    {{#each link in mainControls}}
      {{link-to link.name link.route class=link.classes}}
    {{/each}}
    

    More details abut this approach can be found here.

提交回复
热议问题