在Angular中,RelativeLinkResolution选项用于确定相对路径链接的解析方式。在Angular中,有两种RelativeLinkResolution选项:Legacy和Corrected。
@NgModule({
imports: [
RouterModule.forRoot(routes, { relativeLinkResolution: 'legacy' })
],
exports: [RouterModule]
})
export class AppRoutingModule { }
在传统的RelativeLinkResolution模式下,默认情况下,相对路径链接将相对于导航到它们的URL进行解析。
@NgModule({
imports: [
RouterModule.forRoot(routes, { relativeLinkResolution: 'corrected' })
],
exports: [RouterModule]
})
export class AppRoutingModule { }
在修正版的RelativeLinkResolution模式下,相对路径链接将相对于根URL进行解析。这意味着无论当前URL是什么,相对路径链接都将始终相对于根URL进行解析。
示例代码:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
App Component
Home
`
})
export class AppComponent { }
import { Component } from '@angular/core';
@Component({
selector: 'app-home',
template: `Home Component
`
})
export class HomeComponent { }
根据上面的示例代码,假设我们的根URL是http://example.com/app/
。
在传统的RelativeLinkResolution模式下,如果我们导航到http://example.com/app/
,然后点击Home链接,将会导航到http://example.com/app/home
。
在修正版的RelativeLinkResolution模式下,无论当前URL是什么,点击Home链接都会导航到http://example.com/app/home
。
这就是Angular中RelativeLinkResolution选项的传统和修正版之间的差异。根据你的需求,选择适合你的模式。