angular - 直接在 Angular 6 中加载页面

标签 angular

我有一个 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/

相关文章:

css - ionic 3元素中的 Font Awesome 安装

javascript - Angular - 检测两个订阅何时更新

angular - 暂停和恢复可观察流,请提出更好的选择

angular - 错误 : Please, 将您的依赖项升级到 core-js@3 的实际版本

Angular Testing - 可观察管道不是函数

javascript - Angular 2 路由 : prevent URL from changing

angular - 显示单个 Observable,而不是数组

Angular 4 : Jasmine: Failed: Uncaught (in promise): TypeError: Cannot read property 'nativeElement' of null

angular - 如何在 Protractor 中执行 Control+Click 多选选项?

html - 为什么媒体查询在 Angular 8 中不起作用