我有一个Angular应用程序,通过使用localhost:4200
在ng serve
上可以正常工作;节点服务器位于localhost:3000
。
当我执行ng build
时,由于localhost:3000
正在创建捆绑文件并在app.use(express.static(path.join(__dirname, 'path/to/dist')));
正确提供该文件,但是在localhost:4200/user/id
工作的路由在localhost:3000/user/id
给出错误,表明它为Cannot GET /user/id
。
任何想法导致问题的原因是什么?我在<base href="/">
文件中包含了index.html
。
最佳答案
听起来好像是您的路由位置策略导致了此问题。
直接来自Google的注释:
Angular 4 documentation
网址取决于您选择的策略:
PathLocationStrategy:本地主机:3000 /用户/ ID
HashLocationStrategy:本地主机:3000 /#/用户/ ID
您可以在应用模块中修改useHash属性以在两者之间切换:
@NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(routes, { useHash: true }) // .../#/user/id
], ...
});
也就是说,如果URL不匹配,则GET应该会失败。
关于javascript - Angular 4-所有路线均无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47373632/