加載子站具有類似的方法來處理在轉(zhuǎn)換期間發(fā)生的錯誤。默認(rèn)的加載事件處理程序是一個實現(xiàn),錯誤處理程序發(fā)現(xiàn)一個錯誤子狀態(tài)進(jìn)入。當(dāng)錯誤捕獲時,路由器立即轉(zhuǎn)換子狀態(tài)以處理錯誤。
錯誤事件可以用于通過重定向到指定的頁面來處理和顯示錯誤消息。您還可以使用具有錯誤處理的動態(tài)路由。
Router.map(function() { this.route('temp1', function() { this.route('temp2'); }); });
<!DOCTYPE html> <html> <head> <title>Emberjs error Substates</title> <!-- CDN's --> <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script> <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script> <script src="https://builds.emberjs.com/release/ember.debug.js"></script> <script src="https://builds.emberjs.com/beta/ember-data.js"></script> </head> <body> <script type="text/x-handlebars" data-template-name="index"> <h1>{{data}}</h1> </script> <script type="text/x-handlebars" data-template-name="loading"> <!-- this is the message for the laoding event --> <h1>Loading Contents Please Wait.......</h1> </script> <script type="text/x-handlebars" data-template-name="error"> <!-- this is the message for the error event --> <h2><font color="red">Failed To Load The Contents Please Try Again Later...</font></h2> </script> <script type="text/javascript"> App = Ember.Application.create(); App.IndexRoute = Ember.Route.extend({ model: function(){ return new Promise(function(resolve, reject){ //setting up the time out for data display in mili sec window.setTimeout(function(){ data = "Manu"; //Failure of promise data reject(data); }, 2000); }); }, actions: { error: function(transition, originalRoute){ //Return true to bubble this event to 'loading' or 'indexRoute' return true; } } }); </script> </body> </html>
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
將上面的代碼保存在routing_err_subst.html文件中
在瀏覽器中打開此HTML文件。
更多建議: