Angular路由:从基础到实战
1. 引入Angular路由
在创建新的Angular应用时,Angular CLI会默认添加 <base>
标签,并将 href
值设置为应用程序的根路径 /
。如果应用程序位于不同的文件夹中,需要根据该文件夹的名称进行更改。
1.1 导入路由模块
Angular路由库包含 RouterModule
,这是一个Angular模块,我们需要将其导入应用程序才能开始使用路由功能:
import { RouterModule } from '@angular/router';
我们在主应用程序模块 AppModule
中使用 forRoot
模式导入 RouterModule
:
@NgModule({
imports: [
RouterModule.forRoot(routes)
]
})
RouterModule
的 forRoot
方法返回一个Angular模块,其中包含一组与路由相关的Angular构件:
- 用于执行常见路由任务(如导航)的服务。
- 可在组件中使用的指令,