简体   繁体   English

Angular 4-所有路线均无效

[英]Angular 4 - All routes not working

I have an Angular app working properly at localhost:4200 by using ng serve ; 我有一个Angular应用程序,可以通过ng servelocalhost:4200正常工作; The node server is at localhost:3000 . 节点服务器位于localhost:3000

When i do ng build a bundle file is being created and served properly at localhost:3000 because of app.use(express.static(path.join(__dirname, 'path/to/dist'))); 当我执行ng build时,由于app.use(express.static(path.join(__dirname, 'path/to/dist')));正在创建一个捆绑文件,并在localhost:3000正确提供该文件app.use(express.static(path.join(__dirname, 'path/to/dist'))); but the routes which worked at localhost:4200/user/id is giving an error at localhost:3000/user/id stating it Cannot GET /user/id . 但是在localhost:4200/user/id上工作的路由在localhost:3000/user/id给出了一个错误,指出它Cannot GET /user/id

Any ideas what is causing the problem? 任何想法导致问题的原因是什么? I have included <base href="/"> in the index.html file. 我在index.html文件中包含了<base href="/">

Sounds like your Routing Location Strategy is causing the issue. 听起来好像是您的路由位置策略导致了此问题。

Notes straight from Google: 直接来自Google的注释:
Angular 4 documentation Angular 4文档

URL depending on your chosen strategy: 网址取决于您选择的策略:
PathLocationStrategy: localhost:3000/user/id PathLocationStrategy:本地主机:3000 /用户/ ID
HashLocationStrategy: localhost:3000/#/user/id HashLocationStrategy:本地主机:3000 /#/用户/ ID

You can modify useHash property in your app module to switch between the two: 您可以在应用模块中修改useHash属性以在两者之间切换:

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    RouterModule.forRoot(routes, { useHash: true })  // .../#/user/id
  ], ...
});

That said, if you mismatch the URLs, your GET should fail. 也就是说,如果URL不匹配,则GET应该会失败。

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

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