You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 17中Router Outlet无法正常工作的问题求助

Angular 17路由无法生效问题排查

我正在学习Angular路由相关内容,但在Angular 17中无法实现路由功能。

我修改了app.routes如下:

export const routes: Routes = [
  { path: '',   redirectTo: '/hello', pathMatch: 'full' },
  { path: '/hello', component: HelloComponent }
];

我的app.component代码:

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule, RouterOutlet, RouterLink, RouterLinkActive, MatButtonModule, MatIconModule, MatListModule, MatSidenavModule, MatToolbarModule, MatToolbarModule, MatSidenavModule, AsyncPipe],
  templateUrl: './app.component.html',
  styleUrl: './app.component.css'
})
export class AppComponent {
  title = 'test-angular';
}

我已经在app.component.html中添加了<router-outlet>标签,但始终看不到HelloComponent的内容。请问我遗漏了什么配置?

以下是我的package.json内容:

{
  "name": "test-angular",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^17.0.0",
    "@angular/cdk": "^17.0.4",
    "@angular/common": "^17.0.0",
    "@angular/compiler": "^17.0.0",
    "@angular/core": "^17.0.0",
    "@angular/forms": "^17.0.0",
    "@angular/material": "^17.0.4",
    "@angular/platform-browser": "^17.0.0",
    "@angular/platform-browser-dynamic": "^17.0.0",
    "@angular/router": "^17.0.0",
    "rxjs": "~7.8.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.14.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^17.0.7",
    "@angular/cli": "^17.0.7",
    "@angular/compiler-cli": "^17.0.0",
    "@types/jasmine": "~5.1.0",
    "jasmine-core": "~5.1.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.2.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.1.0",
    "karma-jasmine-html-reporter": "~2.1.0",
    "typescript": "~5.2.2"
  }
}

问题解决要点

1. 修正路由路径格式

Angular路由的path字段不需要以/开头,当前配置中的path: '/hello'是错误写法,需改为path: 'hello',同时redirectTo的值也应去掉前缀/。修正后的路由配置:

export const routes: Routes = [
  { path: '', redirectTo: 'hello', pathMatch: 'full' },
  { path: 'hello', component: HelloComponent }
];

2. 确认HelloComponent为独立组件

Angular 17默认采用独立组件模式,需确保HelloComponent标记了standalone: true,并按需导入依赖模块:

@Component({
  selector: 'app-hello',
  standalone: true,
  imports: [CommonModule], // 根据组件依赖添加对应模块
  templateUrl: './hello.component.html',
  styleUrl: './hello.component.css'
})
export class HelloComponent {}

3. 检查main.ts中的路由配置

确认main.ts中通过provideRouter传入了路由配置,这是路由生效的核心步骤,示例代码如下:

import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { provideRouter } from '@angular/router';
import { routes } from './app/app.routes';
import { provideAnimations } from '@angular/platform-browser/animations';

bootstrapApplication(AppComponent, {
  providers: [
    provideRouter(routes),
    provideAnimations() // 使用Angular Material时需添加该配置
  ]
});

内容的提问来源于stack exchange,提问作者Manuelarte

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 21:28:35