Angular支持两种路由模式:HashLocationStrategy和PathLocationStrategy。默认情况下,它使用HashLocationStrategy,这意味着Angular路由器将应用程序URL转换为#后面的哈希片段,这不会触发页面刷新。
要使用PathLocationStrategy,首先需要在应用程序模块中导入“LocationStrategy”和“PathLocationStrategy”类,并将它们添加到提供商数组中:
import { NgModule } from '@angular/core'; import { LocationStrategy, PathLocationStrategy } from '@angular/common';
@NgModule({ providers: [ { provide: LocationStrategy, useClass: PathLocationStrategy } ] }) export class AppModule {}
然后,在主HTML文件中添加一个base标签以指定应用程序的基本URL,如下所示:
现在,当刷新页面时,路由器将使用浏览器的PathLocationStrategy,并重新加载相应的组件和数据。