无法使 Router Outlet 在 Angular 17 中不起作用

问题描述 投票:0回答:1

我正在遵循 https://angular.io/guide/router 中的教程,但我无法在 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 标签,但由于某种原因我看不到 hello 组件。

知道我错过了什么吗?

这是我的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"
  }
}
angular angular2-routing
1个回答
0
投票

问题是开头的/。感谢@sinanspd

© www.soinside.com 2019 - 2024. All rights reserved.