我已使用以下代码在app.module.ts文件中尝试过LocationStrategy
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
@NgModule({
imports: [
...
],
declarations: [
...
],
bootstrap: [...],
providers: [
{ provide: LocationStrategy, useClass: HashLocationStrategy }
]
})
In app.component.ts file i have added below code
import {Location} from '@angular/common';
export class AppComponent {
constructor(location: Location) {
location.go(location.path());
}
}
但是如果刷新页面或按f5键时页面无法重新加载,而是导航到其他页面或同一页面时无法识别,而是出现404页面未找到错误。请您告诉我解决方案。
在通过goolge找到许多解决方案后,我尝试使用php在我的角度应用程序中遇到了同样的问题,我尝试了这一方法-对我来说工作正常。
只需将.htaccess文件放入您的根目录并添加以下代码-
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.html [QSA,L]
</IfModule>