本文介绍了带有args的Emberjs路由在某些情况下失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,路由网址出现问题。这是我的路由器:

In some case I got an issue with the routing url. Here's my router :

contacts: Em.Route.extend({
    route: '/contacts',

    index: Em.Route.extend({
        route: '/',
        connectOutlets: function(router, context) {
            App.contactsController.populate()
            var appController = router.get('applicationController');
            appController.connectOutlet('contactsList');
        }
    }),

    show: Em.Route.extend({
        route: '/:contactid',
        connectOutlets: function(router, context) {
            alert('show contact');
        }
    }),

    doShowContact: function(router, event){
        router.transitionTo('show', {contactid: event.context.id});
    }
}),

当我在doShowContact中输入时,如果我将'contactid'指定为上下文,并将'/:contactid'指定为'sho w’,例如,浏览器网址中会显示’/ contacts / 3’,一切正常。

When I enter inside doShowContact, if I specify 'contactid' as context and '/:contactid' as route inside 'show', I'll get for example '/contacts/3' in the browser url, everything is ok.

但是在doShowContact中,如果我指定'contact_id'而不是'contactid'作为上下文,并且 '/:contact_id'而不是'/:contactid'作为路线。我将在浏览器URL中得到 // contacts / undefined'

However in doShowContact, if I specify 'contact_id' rather than 'contactid' as context and '/:contact_id' rather than '/:contactid' as route. I'll get '/contacts/undefined' in the browser url.

有没有办法解释?谢谢!

Is there a way to explain it ? Thanks!

推荐答案

您应该简单地传递联系人实例,而不用 contactid 属性:

You should simply pass the contact instance, not forge an object with contactid property:

doShowContact: function(router, event) {
  var contact = event.context;
  router.transitionTo('show', contact);
}

还应指定 modelClass 您的路线中的属性:

You should also specify the modelClass property in your route:

show: Em.Route.extend({
  route: '/:contact_id',
  modelClass: App.Contact,

  // ...
})

这篇关于带有args的Emberjs路由在某些情况下失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-20 23:11