如果使用Ng构建,则无法导航到指定的URL

时间:2017-06-21 06:22:08

标签: routing angular2-routing angular-cli

我有 Angular 2 应用程序。我在localhost上运行。我在 url:localhost:5050 上有一个页面。如果我直接粘贴url localhost:5050 / test2我在浏览器页面上出现错误“无法GET / test2 ” 这是我的路线

        { path: '', component: Test1Component },
        { path: 'test2', component: Test2Component },
        { path: 'test3', component: test3lComponent},

我不知道这个问题。我必须向客户发送一个链接(链接包含特定页面https://xx.xx.xx.xx/test2的应用程序的URL)。所以客户可以从链接导航到实际页面

更新:如果我使用 ng服务并在 localhost:4200 / test2 上运行,则可以运行(路由) 但如果我使用 ng build 并在 localhost:5050 / test2 上运行,我会无法GET / test2 错误

下面是我的server.js

    app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
   res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, 
   Content-Type, Accept, X-Trace-Id, X-Egress-Time, X-Ingress-Time");
   res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
next();
  });
  app.use(express.static(path.join(__dirname, '/dist')));

2 个答案:

答案 0 :(得分:0)

我认为它实际上是您服务器的一个问题,您当前正在向您的服务器发送一个get请求,该请求尝试匹配不存在的localhost:5050/test2。如果您更改应用程序以便所有未知路由返回基本应用程序,则angular将处理其余部分并在URL上重新路由您的应用程序。

即表达类似:

  app.use('/*', express.static('./dist/index.html'));

答案 1 :(得分:0)

我遇到了同样的问题,并遵循下面链接中的文档。

https://angular.io/guide/deployment

我正在使用Apache服务器,因此修改了.htaccess中的重接线规则

RewriteEngine On
    # If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

我希望这会有所帮助。