简体   繁体   English

角度2:导航栏图标颜色根据路线变化

[英]Angular 2: navbar icon color change based on route

UPDATED ANSWER 更新的答案

See below for the original question. 请参阅以下原始问题。 But thanks to @Smit for leading me to the solution. 但是,感谢@Smit带领我提出了解决方案。

Here is the updated Plunker with the solution: Updated Plunker 这是带有解决方案的更新的Plunker更新的Plunker

Now there may be a better way to solve this, but this is how I have for the time being. 现在可能有更好的方法来解决此问题,但这是我目前的方法。

app.component.ts app.component.ts

import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router'

@Component({
  selector: 'my-app',
  template: `
  <nav class="navbar navbar-toggleable-md navbar-light bg-faded">
      <div>
        <ul class="navbar-nav mr-auto">
          <li class="nav-item">
            <a class="nav-link" routerLink="home">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" routerLink="red">Red <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" routerLink="blue">Blue <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link"><i [ngClass]="setClasses()" class="icon-emotsmile icons"></i></a>
          </li>
        </ul>
      </div>
    </nav>
    <div class="container-fluid">
    <h1>Please click on links in navbar.</h1>
    <hr><hr>
    <router-outlet></router-outlet>
    </div>
  `
})
export class AppComponent implements OnInit {

  someProperty = true;
  anotherProperty = true;

  setClasses() {
      let classes =  {
          redText: this.someProperty,    
          blueText: this.anotherProperty, 
      };
      return classes;
  }

  constructor(private router: Router) {
        router.events.subscribe(e => {
          if(e instanceof NavigationEnd) {
            if(this.router.url === '/' || this.router.url === '/home') {
              this.someProperty=false;
              this.anotherProperty=false;
            console.log("I am home")
          }
          else if(this.router.url === '/red') {
            this.someProperty=true;
            this.anotherProperty=false;
            console.log("I am red")
          }
          else if(this.router.url === '/blue') {
            this.someProperty=false;
            this.anotherProperty=true;
            console.log("I am blue")
          }
          else {
            console.log("wrong")
          }
          }
        });
      }
  ngOnit() {}
}

Basically I used Router/NavigationEnd to find the router url and from there I used ngClass to add/remove classes. 基本上,我使用Router / NavigationEnd查找路由器的URL,然后使用ngClass来添加/删除类。

EDITED on 02/21/2017 于02/21/2017编辑

I'm trying to change the color of the navbar icons when the user is viewing certain pages. 我正在尝试在用户查看某些页面时更改导航栏图标的颜色。

For example, if the user is viewing the home page, the icons should be grey, but when the user if viewing the red page the icons will be red. 例如,如果用户正在查看主页,则图标应为灰色,但是当用户查看红色页面时,图标将为红色。

I am using Angular 2 and Bootstrap 4. Here is the basic setup. 我正在使用Angular 2和Bootstrap4。这是基本设置。

app.component.ts app.component.ts

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

@Component({
  selector: 'my-app',
  template: `
  <nav class="navbar navbar-toggleable-md navbar-light bg-faded">
      <div>
        <ul class="navbar-nav mr-auto">
          <li class="nav-item">
            <a class="nav-link" routerLink="home">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" routerLink="red">Red <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" routerLink="blue">Blue <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link"><i class="icon-emotsmile icons"></i></a>
          </li>
        </ul>
      </div>
    </nav>
    <div class="container-fluid">
    <h1>Please click on links in navbar.</h1>
    <hr><hr>
    <router-outlet></router-outlet>
    </div>
  `
})
export class AppComponent implements OnInit {
  constructor() {}
  ngOnit() {}
}

app-routing.module.ts APP-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { RedComponent }   from './red.component';
import { BlueComponent }   from './blue.component';
import { HomeComponent }   from './home.component';

const routes: Routes = [
  { path: 'red', component: RedComponent },
  { path: 'blue', component: BlueComponent },
  { path: 'home', component: HomeComponent },
  { path: '**', redirectTo: '/'}
];

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

home.component.ts home.component.ts

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

@Component({
  selector: 'my-home',
  template: `
    <h2>HOME</h2>

    <p>I want the navbar to be <b>grey</b> and the smile icon to be <b>grey</b>.</p>
  `
})
export class HomeComponent {}

red.component.ts and blue.component.ts are basically the same as home.component.ts . red.component.tsblue.component.ts是基本相同home.component.ts。

I have created a plunker: Plunker 我创造了一个矮人: Plunker

Alright, after a bit research i found the following to detect changes to the url path. 好吧,经过一番研究,我发现以下内容可以检测到URL路径的变化。

import { Router } from '@angular/router'

    constructor(private router: Router) {
        router.events.subscribe((val) => console.log(val));
      }

This is subscribe all the changes on the url path. 这是对URL路径上的所有更改进行预订。

And in the console you can see the following object. 在控制台中,您可以看到以下对象。

NavigationEnd {id: 1, url: "/newExt", urlAfterRedirects: "/newExt"}

upon redirecting to different extension 重定向到其他扩展名时

NavigationEnd {id: 2, url: "/", urlAfterRedirects: "/"}

This way u can do what u want with ur specific extensions. 这样,您就可以使用自己的特定扩展名完成自己想做的事情。

PS: I placed the constructor in the app.component where the router-outlet lies. PS:我将构造函数放在路由器出口所在的app.component中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM