如何在Ember中获取路径类型

How to get the type of route path in Ember

本文关键字:获取 路径 类型 Ember      更新时间:2023-09-26

如果我在路由器映射中有:

this.resource('detail', { path: '/detail/:type' }, function() {
    ...
});

我在我的Ember应用程序代码中检索currentPanth:

currentPath: '',
ApplicationController : Ember.Controller.extend({
    updateCurrentPath: function() {
        App.set('currentPath', this.get('currentPath'));
        console.log('currentPath',App.currentPath);
    }.observes('currentPath') 
}),

当我在我的应用程序中导航时,我会通过控制台获取路线名称,但当它是"detail"时,我就会获得"detail.index"。我如何获取类型?

您只能访问路由中的参数,即在定义模型时:

App.Router.map(function() {
  this.resource('photo', { path: '/photos/:photo_id' });
});
App.PhotoRoute = Ember.Route.extend({
  model: function(params) {
    return Ember.$.getJSON('/photos/'+params.photo_id);
  }
});

或者你也可以使用paramsFor,也只是在路线中。

根据您试图简化的内容,查询参数可能更适合