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

标签 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;
}

应用程序路由.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/

相关文章:

css - 在 CSS3 中以径向渐变增加圆的大小

javascript - css:没有 <table> 的图库 View

php - 左边距在文本中不能正常工作

Angular2 无法使用prototype.js

javascript - 在页面(哈希)链接中,获取第一个可见元素

javascript - 在管道中重用变量的功能方法

Angular 8 - 如何从回调路由中的 url 获取数据

javascript - 尝试打印 html 文档时无法读取 null 的属性 'document'

javascript - 将表单多次发布到隐藏的 iframe

javascript - 将 contextPath 获取到外部 JavaScript