How to handle 'no route matched' in Ember.js and show 404 page?

后端 未结 5 398
无人共我
无人共我 2020-12-08 18:55

How can I handle the error

Uncaught Error: No route matched the URL \'...\'

and show a custom 404 page?


Note: This questi

5条回答
  •  一整个雨季
    2020-12-08 19:22

    Solution 1

    To display 404 content:

    App.Router.reopen({
            handleURL: function (url) {
                try {
                    return this._super(url);
                } catch (error) {
                    if (error.message.match(/No route matched the URL/)) {
                        return this._super('/404');
                    }
                }
            }
        });
    

    If you want to URL changes to 404 as well:

    App.Router.reopen({
            location: locationImplementation,
            handleURL: function (url) {
                try {
                    return this._super(url);
                } catch (error) {
                    if (error.message.match(/No route matched the URL/)) {
                        this.transitionTo('404');
                        return this._super('/404');
                    }
                }
            }
        });
    

    To understand what happened here see line 22636 in ember rc2.

    Solution 2

    Parse current URL and check if route or resource exist using App.Router.router.recognizer.hasRoute('route.path.goes.here');

提交回复
热议问题