Home route in ui-router

后端 未结 4 1781
陌清茗
陌清茗 2021-02-13 18:11

I use https://github.com/angular-ui/ui-router library. When I try to access index route (\'/\') I\'m redirected to 404. The code:

angular.module(\'cr\').config(f         


        
4条回答
  •  鱼传尺愫
    2021-02-13 18:55

    There is nothing wrong with your code. You are just missing an explicit state for 404. Try adding this:

    .state('404', {
        url: '{path:.*}',
        templateUrl: 'views/404'
    });
    

    To get rid of the hash (#) symbol you need to inject one more dependency into your config module:

    $locationProvider
    

    And use the .html5Mode() method to set HTML5 Mode to true, like so

    $locationProvider.html5Mode(true);
    

    Also, ensure your server is configured to allow Angular to handle your routing. For example, here is a Node/Express configuration that allows the above technique to work:

    app.get('*', routes.index);
    

    And in your index.js file (or however you configure your node.js instance):

    exports.index = function(req, res){
       res.render('index');
    };
    

提交回复
热议问题