简体   繁体   English

Ember.js - Pod结构中的错误状态

[英]Ember.js - Error States in Pod structure

Here is my structure in inside àpp/pods : 这是我在àpp/pods里面的结构:

|-application
|-index
|-error
|-user
||-index
||-view
||-edit

When a error occurs, ember does not load the error route. 发生错误时,ember不会加载error路由。 Instead it tries to load a sub-route like index_error or user_error but these do not exist. 相反,它尝试加载像index_erroruser_error这样的子路由,但这些子路径不存在。

How do i force Ember to load the root error route on any error? 如何强制Ember在任何错误上加载根error路由?

Ember v2.1 Ember-Cli v1.13.8 Ember v2.1 Ember-Cli v1.13.8

I've managed to show an error route inside a pod structure like the following: 我已设法在pod结构中显示错误路由,如下所示:

-app
--pods
----something
------template.hbs
------route.js
----error
------template.hbs

If I throw an error on something/route.js like the following: 如果我在某些/ route.js上抛出错误,如下所示:

export default Ember.Route.extend({

  model() {
   throw new Error('AAA');
 }

});

And have the error/template.hbs with the content: 并将error/template.hbs与内容一起使用:

Arrrh, errror!!!!

It displays the error message. 它显示错误消息。

If you wanted a error sub route for something I think you would need the following: 如果你想为一个错误的子路径something ,我认为你会需要以下条件:

-app
--pods
----something
------template.hbs
------route.js
------error
--------template.hbs
----error
------template.hbs

The structure you've provided should actually do exactly what you're describing you're after. 你提供的结构实际上应该完全按照你所描述的那样去做。

Take a look at this twiddle to see an example. 看看这个旋转 ,看一个例子。 Clicking 'View User' will transition to the user.view route, but clicking 'Edit User' will raise an exception in the user.edit route and instead land you on the error route. 单击“查看用户”将转换到user.view路由,但单击“编辑用户”将在user.edit路由中引发异常,而是将您置于error路由上。

One thing to note is that you should not add the error route yourself in router.js . 需要注意的一点是,您不应该router.js自己添加错误路由。 It receives the transition error as its model, so if you manually do this.route('error') and don't give it a dynamic segment, the transition will fail. 它接收转换错误作为其模型,因此如果您手动执行此this.route('error')并且不为其提供动态段,则转换将失败。


If you want more control over exactly what happens when an error occurs during any transition, you can implement the error action on your application route. 如果您希望更准确地控制在任何转换期间发生错误时发生的情况,您可以在应用程序路由上实现error操作。

actions: {
  error(thrownError) {
    this.transitionTo('my-error-route'); // Or whatever other handling you want
  }
}

You can see a full example of such an arrangement in this twiddle . 你可以在这个旋风中看到这种安排的完整例子。 Note that this is subtly different from the default error behavior in that it will produce a full transition (ie the URL will be updated) rather than just moving into a substate. 请注意,这与默认错误行为略有不同,因为它将产生完整转换(即URL将被更新),而不是仅仅移动到子状态。

http://guides.emberjs.com/v2.1.0/routing/loading-and-error-substates/ http://guides.emberjs.com/v2.1.0/routing/loading-and-error-substates/

According to the above app/application/error is the default in a pod structure. 根据上面的app/application/error是pod结构中的默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM