模板替换历史记录条目

您可以使用 link-to 帮助程序在路由之间移动时将条目添加到浏览器的历史记录中,并使用 replace=true 选项替换当前条目。

语法

{{#link-to 'link-text' 'route-name' replace = true}}
//text here
{{/link-to}}

示例

该示例展示了如何替换浏览器历史记录中的当前条目。创建一个名为 info 的路由,然后打开 router.js 文件以定义 URL 映射 −

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

const Router = Ember.Router.extend ({
   location: config.locationType,
   rootURL: config.rootURL
});

Router.map(function() {
   this.route('info');
});

export default Router;

使用以下代码打开在 app/templates/ 下创建的文件 application.hbs 文件 −

//将 replace = true 选项设置为替换浏览器历史记录条目
{{link-to 'Click For Fruits List' 'info' replace = true}}
{{outlet}}

当您点击"Click For Fruits List"链接时,页面应打开 info.hbs 文件,其中包含以下代码 −

<ul>
   <li>Orange</li>
   <li>Banana</li>
</ul>
{{outlet}}

输出

运行 ember 服务器;您将收到以下输出 −

Ember.js Template Replace History

当您单击 Click For Fruits List 时,它将显示来自模板文件的以下文本 −

Ember.js Template Replace History

emberjs_template.html