4.8 Routing -- Specifying The URL Type

1. 默认的路由器使用浏览器的hash来加载应用程序的开始状态并且当你移动时同步保持。目前,这依赖于浏览器中存在的hashchange事件。

2. 假设下面的路由器,输入/#/posts/new将会把你带到posts.new路由。

app/router.js

Router.map(function() {
  this.route('posts', function() {
    this.route('new');
  });
});
  • 如果你想移除开头的/#使URL使简单的/posts/new,你可以告诉路由器使用浏览器的history API。

3. 记住服务器必须为Ember app服务,来自Router.map function中定义的所有URL。

app/router.js

Ember.Router.extend({
  location: 'history'
});

4. 通过为location指定auto,你可以告诉Ember去使用基于浏览器支持最好的location选项。Ember将使用history,如果用户的浏览器支持,否则跌回hash。

5. 最后,如果你一点也不希望用户浏览器的URL和你的app交互,你可以完全禁用location API。这对测试很有用,或者当你需要使用你的路由器管理状态,但是暂时不想让它污染URL(例如当你把app嵌入到一个更大的页面)。

app/router.js

Ember.Router.extend({
  location: 'none'
});
原文地址:https://www.cnblogs.com/sunshineground/p/5158060.html