【问题标题】:How to add templates to sub-directories in Ember?如何将模板添加到 Ember 中的子目录?
【发布时间】:2015-11-26 16:30:06
【问题描述】:

我很确定我的问题的答案在某些文档的某个地方,我在官方和非官方网站上阅读了很多页面,在 SO 上有很多相关问题,但是我的 google-fu 似乎还不够发达,无法帮助我找到答案,所以我来了。

在我的团队中,我们将 Ember-cli 用于一些 Web 应用程序。我们有经典的 app/templates 文件夹,其中放置了我们拥有的每个“控制器”的 .hbs。在这些车把模板中,我们使用 app/templates/components 中的组件。

我们最近达到了应用程序/模板/组件太大的地步。我认为将它们拆分为子目录是个好主意,例如

  • app/templates/product1 包含特定于 product1 的组件 + 基本 product1 把手(在 app/router.js 中调用的把手,一个“控制器的 hbs”)。
  • app/templates/product2 包含特定于 product2 的组件 + 基本 product2 把手(在 app/router.js 中调用的把手,一个“控制器的 hbs”)。

但是当我这样做时,它不起作用并显示一个白页:

  • 如果我将“控制器的 hbs”从 app/templates 移动到 app/templates/product1,我会得到一个没有加载任何内容的整个白页。
  • 如果我将组件从 app/templates/components 移动到 app/templates/product1,“控制器的 hbs”会显示,但我的组件应该是一个空块。

我不知道我是否必须为它配置 ember 以递归搜索 app/templates 文件夹中的把手,和/或我是否必须更改 router.js,以及如何调用我的组件以便 ember 知道他们在哪里。

提前致谢,

本杰明

【问题讨论】:

    标签: ember.js ember-cli


    【解决方案1】:

    覆盖templateName 属性。

    App.YourStuffComponent = Ember.Component.extend({
        templateName: 'components/intoDir/your-stuff'
    });
    

    演示:http://emberjs.jsbin.com/jenafa/2/edit?html,js,output

    【讨论】:

    • 我在 app/components 中创建了一个文件夹 product-a,并将我的 js 放入:app/components/product-a/some-component.js。我为我的模板做了同样的事情:app/templates/components/product-a/some-component.hbs 最后在 app.js 中我添加了:App.SomeComponent = Ember.Component.extend({ templateName: 'components/product-a/SomeComponent'}); 但它没有用。我也试过:App.SomeComponent = Ember.Component.extend({ templateName: 'components/product-a/some-component.hbs'}); 同样的错误:白页和断言失败:找不到名为“某些组件”的助手。我做错了什么?
    • 子文件夹中的组件应该可以正常工作。尝试使用不带斜线的文件夹名称。您还需要 Ember 1.9.0+。你用的是什么版本?查看 rwjblue 的评论:github.com/ember-cli/ember-cli/issues/2570
    • 另外,如果那是你真正想要的,那么你的问题已经被问到了:stackoverflow.com/questions/29038704/…
    • 您好!抱歉这个迟到的答案,我专注于其他事情。你给我的链接正是我需要的,我的问题已经被问到了……谢谢你的帮助和你的链接,它现在可以正常工作了!
    猜你喜欢
    • 2014-06-19
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    相关资源
    最近更新 更多