首页 文章

{{content-for 'head'}} Ember-cli

提问于
浏览
29

过去1个月我一直在使用Yeoman余烬发生器,现在,我想尝试一下ember-cli .

我运行生成器并启动应用程序,一切正常 .

ember new my-new-app
ember server

但我想知道怎么做

{{content-for 'head'}}

在app / index.html中有效吗?

在查看http://www.ember-cli.com/#tutorials中的其他示例时,他们都没有使用此特定帮助程序?是因为他们使用旧版本的ember-cli?他们为什么不使用这些内容作为助手呢?

我很确定ember.js没有这个内容 - 默认情况下是helper,所以我猜是ember-cli在某处写的?它在哪里以及它的用途是什么?

另外,当我检查my-new-app页面的元素时,“Welcome to Ember.js”的div标签出现在body标签而不是head标签上?怎么可能? {{心吹}}

(在app / index.html中,{{content-for'head'}}放在head标签内)

2 回答

  • 25

    它最近被添加到了基于this discussion的ember-cli .

    以下是commit的相关代码:

    EmberApp.prototype.contentFor = function(config, match, type) {
      var content = [];
    
      if (type === 'head') {
        content.push(calculateBaseTag(config));
    
        content.push('<meta name="' + config.modulePrefix + '/config/environment" ' +
                     'content="' + escape(JSON.stringify(config)) + '">');
      }
    
      content = this.project.addons.reduce(function(content, addon) {
        if (addon.contentFor) {
          return content.concat(addon.contentFor(type, config));
        }
    
        return content;
      }, content);
    
      return content.join('\n');
    };
    
  • 20

    来自Ember CLI guide

    app / index.html app / index.html文件为您的应用程序奠定了基础 . 这是布局基本DOM结构,设置title属性并完成stylesheet / javascript包含的地方 . 除此之外,app / index.html包含多个挂钩 - {{content-for'head'}}和{{content-for'body'}} - 可以被Addons用于将内容注入应用程序的头部或身体 . 这些挂钩需要保留在原位,以便您的应用程序正常运行,但除非您直接使用特定的插件,否则可以安全地忽略它们 .

    我实际上是在寻找 Welcome to Ember.js 来自哪里(显然是 app\templates\application.hbs ),但首先偶然发现了 content-for 助手 .

相关问题