【问题标题】:Does the application route load automatically and load the application.hbs in an Ember app?应用程序路由是否自动加载并在 Ember 应用程序中加载 application.hbs?
【发布时间】:2016-02-02 11:26:02
【问题描述】:

我有这个作为我的 router.js。请注意,它没有申请路线:

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
  this.route('todos', { path: '/'});
});

export default Router;

当我点击主路径时,我看到了我的 application.hbs 模板,并且 todos.hbs 模板已加载到插座中。这是我的 application.hbs:

<section id="todoapp">
  <header id="header">
    <h1>todos header in application.hbs</h1>
  </header>

  {{outlet}}
</section>

<footer id="info">
  <p>
    Footer in application.hbs. Double-click to edit a todo
  </p>
</footer>

为什么我的 application.hbs 会被加载?

我假设 Ember 也知道将 todos.js 加载到我的路由文件夹中:

import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    let todos = [
      {
        title: 'Learn Ember',
        complete: false,
      },
      {
        title: 'Solve World Hunger',
        complete: false,
      }
    ];
    return todos;
  }
});

这是我的 todos.hbs 模板:

<h2>Todos Template</h2>

<ul>
    {{#each model as |todo|}}
        <li>
            {{todo.title}}
        </li>
    {{/each}}
</ul>

主要问题
1. 为什么我的application.hbs在我打到home route的时候会被加载?
2. export default是什么意思?
3. import Ember from 'ember' 行是做什么的? “余烬”从何而来?

【问题讨论】:

    标签: ember.js


    【解决方案1】:
    1. 应用程序路由在每个 Ember.js 应用程序中加载。见http://guides.emberjs.com/v2.1.0/routing/defining-your-routes/#toc_the-application-route

    2. export default 是 ES6 模块规范的一部分。请参阅http://exploringjs.com/es6/ch_modules.html 当该模块被导入另一个模块时,它会将要返回的对象或变量作为要导入的默认内容。

    3. “ember”命名空间内置于 Ember CLI 中。它的默认导出是 Ember 变量本身,在之前的 Ember 版本中它曾经是一个全局变量。

    【讨论】:

    • 哦,我明白了,这里的意思是Ember使用ES6?
    • 是的,Ember CLI 默认转译 ES6/ES2015。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2013-12-31
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多