繁体   English   中英

将Angular与Express(Node.js)结合使用时处理路由

[英]Handle routes when using Angular combined with Express (Node.js)

我在Angular中有以下配置

.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $routeProvider

        .when('/', {
            templateUrl: '../pages/main.html',
            controller: 'RMController',
            controllerAs: 'rm'
        })

        .when('/:user', {
            templateUrl: '../pages/emp-details.html'
        })

    $locationProvider.html5Mode({
      enabled: true,
      requireBase: false
    });

如果单击main.html某些内容,这将很好地工作,然后将我带到emp-details.html

但是,如果我明确键入localhost:8080/1234 (带有param的emp-details.html ),则express从未听说过该路由。

处理这种关系的最佳方法是什么?

我的路线如下所示:

module.exports = function(app, schema) {

    //Finds all users
    app.get('/api/users', function(req, res){
        schema.getEmployees()

            .then(function(results) {
                res.json(results);
            }, function(err) {
                console.log(err);
                if (err) res.json(err);
            });
    });

    app.get('/api/users/:user', function(req, res) {
         schema.getSpecificEmployee(req.params.user)

            .then(function(results) {
                res.json(results);
            }, function(err) {
                console.log(err);
                if (err) res.json(err);
            });
    });

    //Our default path: index.html
    app.get('*', function(req, res){
       res.sendFile(path.join(__dirname + '/public/index.html'));
    });
}

尝试这样做:

    .when('/:user', {
        url: '/emp-detail',    //add this line
        templateUrl: '../pages/emp-details.html'
    })

现在,如果您执行localhost:8080/#/emp-detail ,则应该看到emp-details.html页面。 通过使用localhost:8080 /#/ xyz,您应该能够访问您的路由。 希望这有效!!!

暂无
暂无

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

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