Angular4 +无法读取未定义

时间:2017-08-08 20:41:55

标签: javascript css angular

处理点击链接和更改屏幕上元素颜色之间的逻辑。在此示例中,我想在单击链接时更改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;
}

APP-routing.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 {}

我不断收到这一个错误,导致我的更改无法应用,也可能导致我的链接无法导航:

  

错误类型错误:无法读取未定义的属性“url”       在Object.eval [as updateDirectives](AppComponent.html:1)       at Object.debugUpdateDirectives [as updateDirectives](core.es5.js:13058)       在checkAndUpdateView(core.es5.js:12238)       在callViewAction(core.es5.js:12603)       在execComponentViewsAction(core.es5.js:12535)       在checkAndUpdateView(core.es5.js:12244)       at callWithDebugContext(core.es5.js:13458)       at Object.debugCheckAndUpdateView [as checkAndUpdateView](core.es5.js:12998)       在ViewRef_.webpackJsonp ... / .. / .. / core/@angular/core.es5.js.ViewRef_.detectChanges   (core.es5.js:10169)       在core.es5.js:4807

Inspector中的来源强调此代码

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

这条线有什么问题?

1 个答案:

答案 0 :(得分:3)

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

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

要这样做,你可以将变量设为私有

constructor(private router: Router){}

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

附注:以上解决方案是

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

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