javascript - Ember 路由和链接结构

标签 javascript ember.js

我刚刚开始学习 ember。目前仍在尝试学习路线和网址。需要知道更好的方法来执行此操作,因为在我的 index.html 模板中,每个 Handlebars 模板中都有重复的代码来管理菜单。

apps.js

App = Ember.Application.create();

App.Router.map(function() {
   this.resource('home', { path: '/' });
   this.resource('students');
   this.resource('about');
});

index.html

<script type="text/x-handlebars" id="home">
  <b>HOME</b>
  {{#link-to 'students'}}Students{{/link-to}}
  {{#link-to 'about'}}About{{/link-to}}

  <h1>Welcome to my Ember Web App</h1>
</script>

<script type="text/x-handlebars" id="students">
  {{#link-to 'home'}}HOME{{/link-to}}
  <b>Students</b>
  {{#link-to 'about'}}About{{/link-to}}

  <h1>Students List</h1>
</script>

<script type="text/x-handlebars" id="about">
  {{#link-to 'home'}}HOME{{/link-to}}
  {{#link-to 'students'}}Students{{/link-to}}
  <b>About{</b>

  <h1>About this web app</h1>
</script>

enter image description here

预先感谢您的帮助。

最佳答案

您应该使用应用程序模板来添加页眉和页脚等内容。 documentation 中有很好的描述。 .

因此,您需要一个如下所示的模板:

<script type="text/x-handlebars">
    {{#link-to 'index'}} HOME {{/link-to}}
    {{#link-to 'students'}}Students{{/link-to}}
    {{#link-to 'about'}}About{{/link-to}}
    <div>
    {{outlet}}
    </div>
</script>

这样,特定路由的模板就会变得明显更小,因为它们会渲染到您在应用程序模板中放置 {{outlet}} 表达式的位置。

<script type="text/x-handlebars" id="students"> 
  <h1>Students List</h1>
</script>

关于javascript - Ember 路由和链接结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25460988/

相关文章:

java - 将session变量数据从jsp传递到java

javascript - 带有 $ 的 Grunt lint 错误

javascript - Ember.js - 如何获取元素的宽度?

javascript - 如何强制 ember 数据在重新加载时重置记录的所有字段?

ember.js - Ember 路由、 Controller 、 View

javascript - HTML5 使用 insertBefore 进行拖放

javascript - 对 vuejs-templates webpack-simple 代码片段感到困惑

javascript - jquery jcarousel 错误 : Uncaught TypeError: Object #<Object> has no method 'jcarousel'

javascript - 在 ember.js 中保存对象时出错 : 'Object X has no method ' save''

ember.js - 没有 Ember 数据的 Ajax - 未捕获的类型错误 : Object #<Object> has no method 'forEach'