使用NginX在服务器上部署Angular2应用程序时,不会加载子组件

我在服务器上将我的Angular2应用程序部署为一个Docker镜像,它由NginX提供。

但有些奇怪的事情发生。 当我在本地运行我的webpack-dev-server时,检查它是否可以正常工作,我得到预期的视图

本地

其中根组件包含描述和链接,第二个包含“顶级post”等内容。

然后,我用NginX将它打包到Docker镜像中,并在服务器上提供。 但不是上面的观点,我得到了这个

服务器

控制台什么也没有

加载子组件的问题可能是什么原因? 下面的configuration。

的index.html

<html> <head> <base href="/"> <title>Zielony</title> <meta charset="utf-8"> <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.en"></script> <script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"></script> </head> <body> <my-app>Loading...</my-app> </body> <script src="/app.js"></script> </html> 

boot.ts

 export const routes: RouterConfig = [ { path: '', redirectTo: 'dashboard', pathMatch: 'full' }, { path: 'posts', component: PostsComponent, canActivate: [LoggedInGuard] }, { path: 'dashboard', component: PostsDashboardComponent }, { path: 'posts/:id', component: PostDetailComponent }, { path: 'login', component: LoginComponent }, { path: 'signup', component: SignUpComponent }, ]; bootstrap(AppComponent, [ HTTP_PROVIDERS, provideRouter(routes), disableDeprecatedForms(), provideForms(), LoggedInGuard, { provide: APP_CONFIG, useValue: POSTS_CONFIG }, UserService, PostService ]); 

app.component.ts

 @Component({ selector: 'my-app', template: ` <h1>My First Zielony Angular 2 App</h1> <nav> <a routerLink="/dashboard" routerLinkActive="active">Post Dashboard</a> <a routerLink="/posts" routerLinkActive="active">Posts</a> </nav> <router-outlet></router-outlet> `, styles: [some_css_here,], directives: [ROUTER_DIRECTIVES,] }) export class AppComponent { } 

询问你是否想要更多

如果你仔细想想,服务器上没有文件或目录/仪表板,所以让应用程序工作的方法是检查404并“redirect”到已知位置,即/index.html

所以,将这个片段添加到你的nginxconfiguration中应该可以做到这一点:

 location / { try_files $uri $uri/ /index.html =404; }