gpt4 book ai didi

javascript - Angular 2 (CLI) 路由不起作用(无法读取未定义的属性 'split')

转载 作者:搜寻专家 更新时间:2023-10-30 22:06:45 24 4
gpt4 key购买 nike

为什么路由仅适用于 AppComponent 之后的第一个导入组件(下面的示例代码:路由仅适用于“PageNonFound”)?另外,我注意到在我的项目中添加路由后出现错误

错误

Error in ./AppComponent class AppComponent - 
inline template:13:42 caused by:
Cannot read property 'split' of undefined

应用程序模块.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule, Routes } from '@angular/router';

import { AppComponent } from './app.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { HomeComponent } from './home/home.component';

const appRoutes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent },
{ path: 'home', component: HomeComponent }
];

@NgModule({
declarations: [
AppComponent,
PageNotFoundComponent,
HomeComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forRoot(appRoutes)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

应用程序组件.ts

import { Component } from '@angular/core';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'testing';
}

App.component.html

<nav class="nav navbar navbar-custom navbar-fixed-top" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" [routerLink]="['/home']">Test</a>
</div>
<div>
<div class="collapse navbar-collapse" id="navbar">
<ul class="nav navbar-nav">
<li><a [routerLink]="['/home']" [routerLinkActive]="active">Home</a></li>
</ul>
</div>
</div>
</div>
</nav>
<router-outlet></router-outlet>

版本:

angular-cli: 1.0.0-beta.28.3
node: 6.9.4
os: darwin x64
@angular/common: 2.4.9
@angular/compiler: 2.4.9
@angular/core: 2.4.9
@angular/forms: 2.4.9
@angular/http: 2.4.9
@angular/platform-browser: 2.4.9
@angular/platform-browser-dynamic: 2.4.9
@angular/router: 3.4.9
@angular/compiler-cli: 2.4.9

最佳答案

使用routerLinkActive="active"

[routerLinkActive]="['active']"

改变路线顺序

const appRoutes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent },
];

关于javascript - Angular 2 (CLI) 路由不起作用(无法读取未定义的属性 'split'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42615110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com