Page reload fails when using Angular Ui Router with Html5 mode enabled

后端 未结 7 1369
醉酒成梦
醉酒成梦 2020-12-03 03:41

I am using Angular UI Router in my angular app and i have enabled HTML5 mode to remove the # form the URL by using $locationProvider in the config.



        
7条回答
  •  盖世英雄少女心
    2020-12-03 04:18

    Kasun, the reason that this is occurring is because you are trying to refresh the page from one of your sub routes (has nothing to do with ui-router).

    Basically if you request www.yourdomain.com/ you likely have your server setup to return index.html which bootstraps your angular app. Once the app has loaded, any further url changes take html5Mode into consideration and update your page via ui-router.

    When you reload your page the angular app is no longer valid as it has not loaded yet, so if you are trying to load a sub route (for example: www.yourdomain.com/someotherpage), then your server does not know how to deal with /someotherpage and likely returns 404 or some other error.

    What you need to do is configure your server to return your angular app for all routes. I primarily use node/express, so I do something like:

    app.get('*', function(req, res, next) {
        // call all routes and return the index.html file here
    }
    

    Note: I usually use something like this as a final catch all, however I also include other routes above it for things like requesting static files, web crawlers, and any other specific routes that need to be handled.

提交回复
热议问题