这是我的 app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HttpModule } from '@angular/http';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CookieService } from 'ngx-cookie-service';
import { HttpClientModule } from '@angular/common/http';
import { NgxSpinnerModule } from 'ngx-spinner';
import { NgFlashMessagesModule } from 'ng-flash-messages';
import { NgxfUploaderModule } from 'ngxf-uploader';
import { NgxUploaderModule } from 'ngx-uploader';
import { NumberDirective } from './number.directive';
import { FileSelectDirective } from 'ng2-file-upload';
import { BsDatepickerModule } from 'ngx-bootstrap/datepicker';
@NgModule({
declarations: [
AppComponent,
FirstPageComponent,
SavePasswordComponent,
LoginPageComponent,
VerifyDetailsComponent,
HomePageComponent,
ViewOfferLetterComponent,
ContactOptionComponent,
SocialLinksComponent,
ContactUsComponent,
CompanyDetailsComponent,
CompanyVisionComponent,
SaveInformationComponent,
AboutUsComponent,
ComponyHistoryComponent,
TestimonialComponent,
AllocateOfficeComponent,
NumberDirective,
FacilitiesComponent,
FirstDayRuleComponent,
CompanyMediaComponent,
HeaderPagesComponent,
TestImageGallaryComponent,
],
imports: [
BrowserModule,
MatProgressBarModule,
HttpModule,
FormsModule,
HttpClientModule,
NgxSpinnerModule,
NgxfUploaderModule,
NgxUploaderModule,
NgFlashMessagesModule.forRoot(),
NgbModule.forRoot(),
BsDatepickerModule.forRoot(),
RouterModule.forRoot(
appRoutes, // { enableTracing: true } // <-- debugging purposes only
),
ModalGalleryModule.forRoot() // <----------------- angular-modal-gallery module import
],
providers: [
GlobalService,
AuthguardGuard,
SuperAdmiApiService,
EmployeeApiService,
CookieService
],
bootstrap: [AppComponent]
})
export class AppModule { }
这是我可以生成错误的组件
import { Component, OnInit, VERSION, NgModule, Injectable } from '@angular/core';
// import {EventModel} from '../../models/EventModel';
import { BrowserModule } from '@angular/platform-browser';
import { EmployeeApiService } from '../../../config-pages/employee-api.service';
import { GlobalService } from '../../../config-pages/global.service';
import { CookieService } from 'ngx-cookie-service';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { GridLayout, Image, PlainGalleryConfig, PlainGalleryStrategy } from 'angular-modal-gallery';
export interface Image {
id: number;
text: string;
}
import * as $ from 'jquery';
import { async } from '../../../../../node_modules/rxjs/internal/scheduler/async';
interface JQuery {
center(): JQuery;
}
@Component({
selector: 'app-test-image-gallary',
templateUrl: './test-image-gallary.component.html',
styleUrls: ['./test-image-gallary.component.css']
})
@Injectable()
export class TestImageGallaryComponent implements OnInit, Resolve<any> {
name: string;
compid: any;
candidateid: any;
Response: any;
gallaryData: any;
responseMessage: any;
imageUrl: any;
Image = [];
isDataAvailable: any;
data: any;
i: any;
asyncResult: any;
htmlToAdd: any;
plainGalleryGrid: PlainGalleryConfig = {
strategy: PlainGalleryStrategy.GRID,
layout: new GridLayout({ width: '86px', height: '86px' }, { length: 3, wrap: true })
};
constructor(private EmployeeApi: EmployeeApiService, private _global: GlobalService, private cookieService: CookieService) {
this.candidateid = this.cookieService.get('candidateid');
this.compid = this.cookieService.get('companyid');
this.imageUrl = this._global.CompanyImagePath;
}
resolve(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
): Observable<any[]> {
this.data = this.EmployeeApi.getimagegallarydata(this.compid, this.candidateid ).pipe(map(
resultArray => {
this.Response = resultArray;
if (this.Response.status === 200) {
this.gallaryData = this.Response.gallarydata;
for ( this.i = 0; this.i < this.gallaryData.length; this.i++) {
// alert(this.i);
this.Image[this.i] =
new Image(
this.i,
{ // modal
img: this.imageUrl + this.gallaryData[this.i].filename,
extUrl: 'http://www.google.com'
}
);
}
console.log(this.Image);
} else {
this.responseMessage = 'Gallary not available';
alert(this.responseMessage);
}
}
)
);
return void(0);
}
ngOnInit() {
this.pageload();
}
// set page ui according to screen size
pageload() {
$.fn.center = function () {
this.css('position', 'absolute');
this.css('top', Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) +
$(window).scrollTop()) + 'px');
this.css('left', Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + 'px');
return this;
};
$('#abc0').center();
}
}
我在角度6中有静态注入问题,实际上我在app.module.ts中导入httpclientmodule而ad在导入数组中
1 回答
看起来您正在尝试将组件用作解析器 .
解析器的目的是服务,因为它们应该在模块的
providers
数组中注册 - 我怀疑这就是你收到注入器错误的原因 .理想情况下,您应该将
resolve
拆分为单独的类,使用@Injectable
进行修饰,并在模块的providers
数组中引用它 .你的解析器看起来像这样:
你的路由器配置看起来像这样:
您的组件可以按如下方式访问数据:
使用组件作为自己的解析器并不是我曾经想过的事情,但我工作了 . 如果将组件添加到
providers
数组,它可能会运行,但数据不会达到您期望的结果 .注入器将提供"component"用于解析为单例,并在视图请求时提供完全不同的组件实例 . 组件在请求时进行新实例化,而
providers
数组中的所有内容都是单例 .angular guide提供了一个如何使用解析器的合理深度示例 .