要给出Angular 10中使用路由的代码示例,需要先安装Angular 10并创建一个新的Angular项目。以下是一个简单的解决方案。
npm install -g @angular/cli
ng new angular-routing-example
cd angular-routing-example
ng generate component home
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Welcome to Home Page
ng serve
这是一个简单的Angular 10路由示例。您可以根据自己的需求添加更多的路由和组件。