在我的图书馆中,我有一个带有以下代码的服务:
import { Inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { DataInjectorModule } from '../../data-injector.module';
// @dynamic
@Injectable()
export class RemoteDataService<T> {
@Inject('env') private environment: any = {};
public type: new () => T;
constructor(
model: T,
) {
this.http = DataInjectorModule.InjectorInstance.get(HttpClient);
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
(现有原因data-injector.module只是避免循环依赖):
import { NgModule, Injector } from '@angular/core';
// @dynamic
@NgModule({
declarations: [],
imports: [],
providers: [],
exports: [],
})
export class DataInjectorModule {
static InjectorInstance: Injector; …Run Code Online (Sandbox Code Playgroud) 请考虑我的应用程序的波纹管图
EventsHub是一个简单的注射服务:
import {Injectable} from '@angular/core';
import {Subject} from 'rxjs/Subject';
@Injectable()
export class EventsHub {
private announcementSource = new Subject<string>();
messageAnnounced$ = this.announcementSource.asObservable();
announce( message : string) {
console.log('eventHub : firing...'+message);
this.announcementSource.next(message);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当从"基金","客户"或路由器插座内的任何其他组件调用"通知"功能时,父(MainApp)将不会收到任何消息.
另一方面,当我从NavigationMenu调用相同的服务功能时,MainApp接收该事件就好了.那么如何让路由组件与其父组件进行交互呢?
谢谢
此案例已在RC1和RC2上进行了测试
我有以下模块声明:
import { ConnectionService } from './services/connection.service';
import { NgModule } from '@angular/core';
import { Http, RequestOptions } from '@angular/http';
import { AuthHttp, AuthConfig } from 'angular2-jwt';
export function authHttpServiceFactory(http: Http, options: RequestOptions) {
return new AuthHttp(new AuthConfig({
tokenName: 'token',
tokenGetter: (() => sessionStorage.getItem('token'))
}), http, options);
}
@NgModule({
providers: [
{
provide: AuthHttp,
useFactory: authHttpServiceFactory,
deps: [Http, RequestOptions]
}
],
})
export class AuthModule { }
Run Code Online (Sandbox Code Playgroud)
和服务:
import { Injectable } from '@angular/core';
@Injectable()
export class ConnectionService {
id; …Run Code Online (Sandbox Code Playgroud) 我有一个 Angular 应用程序,它初始化 Web Worker 并在其中启动一些渲染操作。Worker 有帮助类 Renderer 的实例来执行渲染操作。
现在我需要从 RealtimeDataService (@Injectable) 获取实时数据以提供给渲染器。
但是当我在我的助手类 Renderer 中导入服务(或直接导入到工作器打字稿文件)时,编译器向我抛出这个错误:
./src/app/workers/ecg.worker.ts 中的错误 (./node_modules/worker-plugin/dist/loader.js!./src/app/workers/ecg.worker.ts) 模块构建失败(来自 . /node_modules/worker-plugin/dist/loader.js):错误:../../../node_modules/@angular/core/core.d.ts(1470,29):错误TS2304:找不到名称'元素'。../../../node_modules/@angular/core/core.d.ts(9115,81): 错误 TS2304: 找不到名称“HTMLElement”。../../../node_modules/@angular/core/core.d.ts(10194,62): 错误 TS2304: 找不到名称“节点”。../../../node_modules/@angular/core/core.d.ts(13467,20): 错误 TS2304: 找不到名称“文档”。../../../node_modules/@angular/core/core.d.ts(13481,13): 错误 TS2304: 找不到名称“窗口”。
如何在 Web Worker 上下文中使用可注入服务?
我在flutter中使用了injectable和get_it包,我有一个共享的首选项类:
@LazySingleton()
class SharedPref {
final String _token = 'token';
SharedPreferences _pref;
SharedPref(this._pref);
Future<String> getToken() async {
return _pref.getString(_token) ?? '';
}
Future<void> setToken(String token) async {
await _pref.setString(_token, token);
}
}
Run Code Online (Sandbox Code Playgroud)
这个类作为LazySingleton注入,我有一个模块用于注入共享首选项:
@module
abstract class InjectableModule {
@lazySingleton
Future<SharedPreferences> get prefs => SharedPreferences.getInstance();
}
Run Code Online (Sandbox Code Playgroud)
在集团类 im 中使用 SharedPref 类:
@injectable
class LoginCheckBloc extends Bloc<LoginCheckEvent, LoginCheckState> {
final SharedPref sharedPref;
LoginCheckBloc({@required this.sharedPref}) : super(const LoginCheckState.initial());
@override
Stream<LoginCheckState> mapEventToState(
LoginCheckEvent event,
) async* {
if (event is CheckLogin) { …Run Code Online (Sandbox Code Playgroud) dependency-injection injectable sharedpreferences flutter flutter-dependencies
我在我的 flutter 项目中使用 get_it 进行依赖注入。现在我正在尝试使用可注入来完成此操作,以用注释替换我的手写依赖文件。
我有一个特殊情况,我有一个通用类,必须使用不同的 T 值注入 3 次。
class TestBloc<T> {
...
}
Run Code Online (Sandbox Code Playgroud)
这就是我的旧配置中的样子:
sl.registerFactory(() => TestBloc<One>(...);
sl.registerFactory(() => TestBloc<Two>(...);
sl.registerFactory(() => TestBloc<Three>(...);
Run Code Online (Sandbox Code Playgroud)
我如何注释该类才能使其正常工作?
当我添加@injectable时
@injectable
class TestBloc<T> {
...
}
Run Code Online (Sandbox Code Playgroud)
我明白了(当然):
gh.factory<_i34.TestBloc<dynamic>>(() =>_i34.TestBloc<dynamic>(...);
Run Code Online (Sandbox Code Playgroud)
如何注释才能得到这个?
gh.factory<_i34.TestBloc<One>>(() =>_i34.TestBloc<One>(...);
gh.factory<_i34.TestBloc<Two>>(() =>_i34.TestBloc<Two>(...);
gh.factory<_i34.TestBloc<Three>>(() =>_i34.TestBloc<Three>(...);
Run Code Online (Sandbox Code Playgroud) 我有一个Angular2应用程序,service用于从API获取数据.在这个例子之后,我想创建一个单独的文件,其中应该包含一些配置数据.我的问题是我的服务有一个@Injectable()装饰器,我不确定我是否可以provide在元数据中传递一个数组,我将在其中注入配置,如教程中所示.有人遇到过这样的问题,欢迎分享他的解决方案:)
我的@Injectable应用程序中有一个服务,作为提供程序添加AppModule.我想确保我的开发团队中没有人在任何其他模块中注入它.单个实例就足够了,它有一些复杂的逻辑,我不想运行两次.有任何想法吗?
我知道DI在角度2中是如何工作的,所以像'确保它仅作为提供者添加在App模块中'这样的答案将无济于事.:(
请注意,如果将服务提供给除AppModule之外的任何其他服务,我希望它在构建或运行时产生某种错误.
我正在开发 angular2,我已经创建了服务并在组件中使用 @Inject 注入这些服务。我对 @Injectable() 在服务本身中的使用以及它产生的差异感到困惑。
我有这两个服务文件,其中一个包含在另一个中。
应用程序服务.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { throwError, Observable } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { Router } from '@angular/router';
import { environment } from '../../environments/environment';
import { AuthService } from '../_services/auth.service';
@Injectable({
providedIn: 'root'
})
export class AppService {
protected _apiURL = environment.apiURL;
// Define URLs here
_overviewURL: string;
_deleteUserUrl: string;
constructor(private http: HttpClient,
private _router: Router,
private _authService: AuthService) {
this.setApiUrl();
}
/* …Run Code Online (Sandbox Code Playgroud) injectable ×10
angular ×8
service ×4
typescript ×3
flutter ×2
angular10 ×1
components ×1
generics ×1
inject ×1
interaction ×1
javascript ×1
singleton ×1
testing ×1
web-worker ×1