【问题标题】:How do I retrieve the path of a route?如何检索路线的路径?
【发布时间】:2013-05-09 21:02:20
【问题描述】:

鉴于以下路由器设置:

App.Router.map(function() {
  this.resource('posts', function() {
    this.route('new');
    this.resource('post', { path: ':post_id' }, function() {
      this.route('edit');
    });
  });
});

是否可以获取路由的路径

虚构的例子:

App.Router.get('path', 'posts.new'); //=> "/posts/new"

或者像这样的模型:

App.Router.get('path', 'post.edit', model); //=> "/posts/1/edit"

【问题讨论】:

  • 也许你想看看源代码。 LinkView 实现了

标签: ember.js ember-router


【解决方案1】:

在你的控制台/开发工具中试试这个:

App.Router.router.generate(['posts.new']);

这应该输出:

/posts/new

还有一个模型:

App.Router.router.generate(['post.edit'], postModel);

会输出

/posts/1/edit

感谢@MilkyWayJoe 的参考! https://github.com/emberjs/ember.js/blob/master/packages/ember-routing/lib/helpers/link_to.js#L112-L117 :)

【讨论】:

  • 请记住,如果没有实际对象,这可能不会为动态路由生成 url(它没有要序列化的内容!)。
  • 是的,这行得通!如果你想添加一个模型,只需将它作为第二个参数传递。 App.Router.router.generate(['post.edit'], postModel);谢谢大家=]
  • 这方面的文档在哪里?我找不到它。
猜你喜欢
  • 1970-01-01
  • 2011-05-09
  • 2010-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
  • 1970-01-01
相关资源
最近更新 更多