gpt4 book ai didi

javascript - Angular4 + 无法读取未定义的属性 'url'

转载 作者:行者123 更新时间:2023-11-29 19:02:50 25 4
gpt4 key购买 nike

研究单击链接和更改屏幕上元素颜色之间的逻辑。在此示例中,我想在单击链接时更改 h1 标记的颜色。这是我到目前为止所拥有的。蓝色链接不会导航到其他组件

app.module.ts

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

import { AppComponent } from './app.component';
import { BlueComponent } from './blue/blue.component';
import { AppRoutingModule } from './app-routing.module';

import { routes } from './app-routing.module';

@NgModule({
declarations: [
AppComponent,
BlueComponent
],
imports: [
BrowserModule,
AppRoutingModule,
RouterModule.forRoot(routes)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

import { Component } from '@angular/core';
import { Router, ActivatedRoute, RouterOutlet, Routes } from '@angular/router';
import { BrowserModule } from '@angular/platform-browser';
import { NgClass, CommonModule } from '@angular/common';

@Component({
selector: 'app-root',
styleUrls: ['./app.component.css'],
template: `<h1 [ngClass]="{'red': (router.url === '/'),
'blue': (router.url === '/blue')}">Color Changer</h1>
<a routerLink='/'>Red</a>
<a routerLink='/blue'>Blue</a>
<router-outlet></router-outlet>
`
})
export class AppComponent {
constructor(router: Router) {}
}

app.component.css

.red {
color: red;
}
.blue {
color: blue;
}

应用程序路由.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { AppComponent } from './app.component';
import { BlueComponent } from './blue/blue.component';

export const routes: Routes = [
{ path: '', component: AppComponent },
{ path: 'blue', component: BlueComponent },
];

@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})

export class AppRoutingModule {}

我不断收到此错误,该错误导致我的更改无法应用,并且可能导致我的链接无法导航:

ERROR TypeError: Cannot read property 'url' of undefined at Object.eval [as updateDirectives] (AppComponent.html:1) at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:13058) at checkAndUpdateView (core.es5.js:12238) at callViewAction (core.es5.js:12603) at execComponentViewsAction (core.es5.js:12535) at checkAndUpdateView (core.es5.js:12244) at callWithDebugContext (core.es5.js:13458) at Object.debugCheckAndUpdateView [as checkAndUpdateView] (core.es5.js:12998) at ViewRef_.webpackJsonp.../../../core/@angular/core.es5.js.ViewRef_.detectChanges (core.es5.js:10169) at core.es5.js:4807

Inspector 中的来源在此代码下划线

<h1 [ngClass]="{'red': (route.url === '/'),
'blue': (route.url === '/blue')}">Color Changer</h1>

该行有什么问题?

最佳答案

您需要使路由器可供整个组件使用。

constructor(router: Router){
// router is only available here
}

为此,您可以将变量设为私有(private)

constructor(private router: Router){}

现在路由器可以通过 this.router 在整个组件中使用


旁注:上述解决方案是

的简写
export class AppComponent {
private router: Router;

constructor(router: Router) {
this.router = router;
}
}

关于javascript - Angular4 + 无法读取未定义的属性 'url',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45577687/

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