【问题标题】:Ember.js more than one param in urlEmber.js 网址中有多个参数
【发布时间】:2015-10-13 13:45:33
【问题描述】:

我有一条路线 /changepassword,我需要添加 2 个参数:iduserresetcode。看起来像这样:/changepassword?iduser=123&resetcode=foo

我阅读了与此相关的其他内容(12),但我无法弄清楚。我在 ember 方面没有任何经验,我被要求在我工作的地方做一些修改。我们正在使用 ember 1.5.1

我尝试了this,但它让我失望了:Uncaught Error: Assertion Failed: Error: Assertion Failed: The URL '/changepassword?test1=aaasd' did not match any routes in your application

[编辑]

感谢@GJK,我知道我需要的是查询参数,没有路径参数。

现在,我的代码如下所示:

路线:

module.exports = App.Router.map ->
      ...
      ...
      @resource('change_password', path: '/changepassword')

控制器:

module.exports = App.ChangePasswordController = Ember.Controller.extend

  needs: ['config', 'viewport', 'application']

  queryParams: ['test']
  test: null
  ...
  ...

但是,当我访问http://localhost:3333/#/changepassword?test=foo 时正在转换为:http://localhost:3333/#/changepassword


[旧]

这是我的路线:

module.exports = App.Router.map ->
  @resource 'index', path: '/', ->
  @resource('account_sign_up', path: '/signup')
  @resource('unsubscribe', path: '/unsubscribe')
  @resource('change_password', path: '/changepassword/:test1/:test2')

有什么帮助吗?谢谢!

【问题讨论】:

  • 您似乎想使用query parameters,但您在路由中声明了路径参数。这些具有非常不同的语义。您应该阅读查询参数以查看它们是否适用于您的用例。 (最大的区别是您无法在路由的 model 挂钩中访问它们。)
  • @GJK 但我不需要在我的路线中添加参数?谢谢
  • 不,查询参数是在控制器上声明的,而不是在路由上。
  • @GJK 哦,好的。但是现在如果我访问changepassword?test=foo,该站点会将我重定向到changepassword。可以吗?
  • 您的控制器上是否声明了test 查询参数? Ember 现在管理您的查询参数,它不会允许错误的查询参数留在 URL 中。

标签: javascript ember.js coffeescript


【解决方案1】:

有两种可能的方法来做到这一点:

1) 使用带多个参数的 params 变量:

@resource('change_password', path: '/changepassword/:iduser/:resetcode')

model : function (params) {
  console.log(params.iduser);
  console.log(params.resetcode);    
}

2) 使用http://guides.emberjs.com/v1.10.0/routing/query-params/(我不确定它是否适用于 1.5)..所以我没有包含示例..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2012-07-25
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    相关资源
    最近更新 更多