首页 文章

Ember Ember数据,在应用程序级别处理错误子状态,Ember 2.8

提问于
浏览
2

我正在尝试做一些听起来很简单的事情 . 我想将服务器错误渲染到我的application.hbs的出口 .

阅读这个文档,https://guides.emberjs.com/v2.6.0/routing/loading-and-error-substates/,我能够检测到我的错误,但我似乎无法通过遵循给定的模式将错误作为模型传递给我的error.hbs .

如果我在应用程序路由上注册错误操作处理程序,如上所述,我能够呈现我的error.hbs模板,但我无法访问我正在处理的错误对象的上下文 . 此外,我的URL路由更新,这是不希望的 .

actions: {
    error(err, transition) {
        return this.transitionTo('error');
    }
}

使用这个处理程序,我确实将error.hbs渲染到我的插座,但是我没有从我的错误对象获取任何上下文来渲染到该模板 . 如果我尝试将错误作为模型传递,我会收到此错误:

More context objects were passed than there are dynamic segments for the route: error

现在,只要加载模型发生错误,我就会得到一个永远挂起的加载微调器状态 . 相反,我希望显示服务器错误 .

1 回答

  • 1

    错误作为模型传递给错误子状态 . throw n或 reject ed的对象将是您的错误子状态的模型,其属性将是属性 .

    例如,如果您 throwRSVP.reject { message: 'An Error Occurred' } ,则可以使用 {{model.message}} 在error.hbs中显示该内容 . 我从来没有见过 throw / reject 原语 .

    Here is a twiddle that demonstrates working error substates(加载/ reject和/ undefined路由以查看其他示例) .

    Code from the twiddle

    templates / application-error.hbs(也可能是error.hbs)

    <p>Application-Error Template</p>
    <p>model: {{model}}</p>
    <p>model.message: {{model.message}}</p>
    <p>model.customMessage: {{model.customMessage}}</p>
    

    路线/ throw.js

    model(params) {
      throw { customMessage: 'Reject Error Message' };
    },
    

    路线/ reject.js

    model(params) {
      return Ember.RSVP.reject({ customMessage: 'Reject Error Message' });
    },
    

    路线/ undefined.js

    model(params) {
      use.undefined.value;
    },
    

相关问题