Routing 如果使用Ng生成,则无法导航到指定的url

Routing 如果使用Ng生成,则无法导航到指定的url,routing,angular2-routing,angular-cli,Routing,Angular2 Routing,Angular Cli,我有我的Angular 2应用程序。我正在本地主机上运行。我在url:localhost:5050上有一个页面。如果我直接粘贴url localhost:5050/test2,我会在浏览器页面上出现错误“无法获取/test2” 这是我的路线 { path: '', component: Test1Component }, { path: 'test2', component: Test2Component }, { path: 'test3', c

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

        { path: '', component: Test1Component },
        { path: 'test2', component: Test2Component },
        { path: 'test3', component: test3lComponent},
我对这个问题一无所知。我必须发送一个链接(链接包含一个特定页面的应用程序的url)给客户。因此,客户可以从链接导航到实际页面

更新:如果我使用ng serve并在localhost:4200/test2上运行,它会工作(它会路由) 但是如果我使用ng build并在localhost:5050/test2上运行,我会得到无法得到/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')));

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

i、 e在表达式中类似于:

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

我也面临同样的问题,并遵循下面链接上的文档

我使用的是Apache服务器,因此修改了.htaccess中的重新布线规则

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

我希望这有帮助。

Test2组件中发生了什么?localhost:5050/test2工作正常吗?如果是这样,您是否尝试将test3lComponent替换为Test2Component作为测试?应用程序的流程很好。我可以按顺序正确地从test1路由到test2,再路由到test3。但是当我直接想从浏览器导航到localhost:5050/test2时,我得到了一个错误。它的主要任务是通过指定组件的直接路径从url导航到任意页面。好的,你的路由器文件的其余部分是什么样子的?@BenjiLees下面是我的路由,我的索引是const-appRoutes:routes=[{path:'',component:Test1Component},{path:'test2',component:Test2Component},{path:'Test3:id',component:Test3Component},];我正在使用node app.use(函数(req,res,next){res.header(“访问控制允许来源”,“*”);res.header(“访问控制允许来源”,“来源,X-Requested-With,Content-Type,Accept,X-Trace-Id,X-exgress-Time,X-Ingress-Time”);res.header('Access-Control-Allow-Methods','GET,POST,OPTIONS,PUT,PATCH,DELETE');next();});我已经阅读并尝试了一些不同的方法,这就是我的node
app.use('*',express.static(path.join(u dirname,“/dist'))中的方法
我尝试了您告诉我的方法,但我遇到了此错误
未捕获的语法错误:意外标记您是否考虑过使用类似
express http proxy
的代理?