我有一个 Angular 6 应用程序,其中包含一个名为 assets 的组件。当我通过 routerLinks 导航到组件时,页面加载并按预期显示数据:
http://localhost:4200/assets/2
但是,如果我刷新页面,或者直接在浏览器中加载链接,那么我的很多脚本都无法加载:
GET http://localhost:4200/assets/runtime.js net::ERR_ABORTED 404 (Not Found)
Refused to execute script from '<URL>' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
GET http://localhost:4200/assets/polyfills.js net::ERR_ABORTED 404 (Not Found)
我需要在路由器中做些什么吗?或者是什么原因造成的?
最佳答案
在您的路由模块中启用哈希位置。它应该如下所示。
const routes: Routes = [
{ path: '', component: MyComponent },
{ path: 'my-other', component: MyOtherComponent }
];
@NgModule({
exports: [ RouterModule ],
imports: [ RouterModule.forRoot(routes, {useHash: true}) ]
})
export class MyRoutingModule { }
之后,Angular 将能够处理刷新。
关于angular - 直接在 Angular 6 中加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52555589/