Angular 2手动将服务注入到类中?

时间:2017-03-14 22:01:21

标签: angular angular2-services

我想将一个服务注入一个只有常量的类。

我想注入的服务是:

import {Injectable} from '@angular/core';
import {ActivatedRoute, Router, NavigationStart} from '@angular/router';
import {location} from '@angular/common';

enum Environment {
    local, 
    dev
}

@Injectable()
export class EnvService {
    private _env: Environment = Environment.dev;

    constructor(
        private router: Router,
        private activatedRoute: ActivatedRoute,
        private location: Location
    ) {
        this._env = someApiCallToGetEnv(); 
    }

    getEnv(): string {
        return Environment[this._env];
    }
}

我要注入的课程就是这个......

import {ModuleWithProviders} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';
import {ResourcesComponent} from 'components/resources.ts';

import {EnvService} from 'services/env.service';


const appRoutes: Routes = [
    {
        path: '',
        redirectTo: EnvService.getEnv() + '/resources',
        pathMatch: 'full'
    },
    {
        path: 'resources',
        component: ResourcesComponent,
    }
];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

根据环境的不同,当用户首次导航到网站的根目录时,我希望重定向链接是动态的。

我该怎么做?

0 个答案:

没有答案