大佬教程收集整理的这篇文章主要介绍了angular – 使用可注入服务来设置APP_BASE_HREF的配置,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
import { Injectable } from '@angular/core'; @Injectable() export class AppConfigservice { public config: any = { auth0ApiKey: '<API_KEY>',auth0Domain: '<DOMAIN>',auth0CallBACkUrl: '<CALLBACK_URL>',appBaseHref: '/' } constructor() {} /* Allows you to update any of the values dynamically */ set(k: String,v: any): void { this.config[k] = v; } /* Returns the entire config object or just one value if key is provided */ get(k: String): any { return k ? this.config[k] : this.config; } }
现在我想在app-module.ts上使用该可注入服务来设置APP_BASE_HREF提供程序.
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { httpR_350_11845@odule } from '@angular/http'; import { AppComponent } from './app.component'; import { AppComponent } from './app/app.component'; import { HelpComponent } from './Help/Help.component'; import { WelcomeComponent } from './welcome/welcome.component'; import {APP_BASE_HREF} from "@angular/common"; import { MaterialModule } from "@angular/material"; import { AUTH_PROVIDERS } from "angular2-jwt"; import { RouterModule } from "@angular/router"; import {AppConfigservicE} from "app-config.service"; const appConfigservice = new AppConfigservice(); @NgModule({ declarations: [ AppComponent,HelpComponent,WelcomeComponent ],imports: [ BrowserModule,FormsModule,httpR_350_11845@odule,MaterialModule.forRoot(),RouterModule.forRoot([ { path: "",redirectTo:"welcome",pathMatch:"full"},{ path: "welcome",component: WelcomeComponent },{ path: "Help",component: HelpComponent},{ path: "**",redirectTo:"welcome"} ]) ],providers: [AUTH_PROVIDERS,{provide: APP_BASE_HREF,useValue:appConfigservice.get('appBaseHref')}],bootstrap: [AppComponent] }) export class AppModule { }
所以在这里我将类启动到const并使用它.有没有一种方式注入凉爽和性感的方式?
例如,对于我的auth服务,我在构造函数中定义了它
constructor(@Inject(AppConfigservicE) appConfigservice:AppConfigservicE)
还有办法在这里做一件性感的事吗?或者只是按原样离开?
谢谢
providers: [ AppConfigservice,{ provide: APP_BASE_HREF,useFactory: (config: AppConfigservicE) => { return config.get('appBaseHref') },deps: [ AppConfigservice ] } ]
将AppConfigservice添加到提供程序后,可以将其注入工厂和身份验证服务.这通常是应该如何完成的.稍后如果说AppConfigservice可能需要一些依赖,它将通过注入系统处理.
也可以看看:
> What is the difference between @Inject vs constructor injection as normal parameter in Angular 2?关于你对@Inject的使用.
以上是大佬教程为你收集整理的angular – 使用可注入服务来设置APP_BASE_HREF的配置全部内容,希望文章能够帮你解决angular – 使用可注入服务来设置APP_BASE_HREF的配置所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。