我创建了一个组件来本地预览项目中的docx和pdf文件(角度6)。要预览pdf,我使用了ng2-pdf-viewer并预览了docx,docx文件在后端被转换为html,发送到角度。 docx和pdf的预览在本地计算机上工作正常,但在服务器上不起作用。 控制台中的错误如下所示。在错误中,它提到了组件“ t”。我认为没有这样的组成部分。这可能是另一个错误。
html文件:
<loading-spinner *ngIf="loading"></loading-spinner>
<div *ngIf="!loading">
<!-- for pdf -->
<pdf-viewer *ngIf="isPdf" [src]="pdfSrc" style="display: block;" (after-load-complete)="afterLoading()"
(error)="onError()" (contextmenu)="onRightClick()" [zoom]="zoom">
</pdf-viewer>
<!-- for docx -->
<div class="container" *ngIf="isDocx">
<div [innerHTML]="html" class="doc" (contextmenu)="onRightClick()" (click)="onLeftClick()"></div>
</div>
</div>
<button class="btn btn-light zoomIn" (click)="zoomIn()"><i class="fa fa-search-plus" aria-hidden="true"></i></button>
<button class="btn btn-light zoomOut" (click)="zoomOut()"><i class="fa fa-search-minus" aria-hidden="true"></i></button>
ts文件:
import { Component, OnInit, Directive, ElementRef, Renderer2 } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { environment } from '../../../environments/environment';
import { PublicService } from '../public.service';
declare var jQuery: any;
@Component({
selector: 'app-file-preview',
templateUrl: './file-preview.component.html',
styleUrls: ['./file-preview.component.css']
})
@Directive({ selector: '[preventCutCopyPaste]' })
export class FilePreviewComponent implements OnInit {
fileName;
siteUrl = environment.resourcecUrl + "/";
doc;
loading = true;
isPdf = false;
isDocx = false;
html;
pdfSrc;
zoom = 1;
constructor(
private route: ActivatedRoute,
public publicService: PublicService,
el: ElementRef, renderer: Renderer2
) {
this.route.queryParams.subscribe(params => {
this.fileName = params['fileName'];
this.pdfSrc = this.siteUrl + "upload/project/" + this.fileName;
this.checkFileType();
});
//disable copy
var events = 'cut copy paste';
events.split(' ').forEach(e =>
renderer.listen(el.nativeElement, e, (event) => {
event.preventDefault();
})
);
}
ngOnInit() {
}
zoomIn() {
this.zoom += 0.1;
console.log(this.zoom);
}
zoomOut() {
this.zoom -= 0.1;
console.log(this.zoom);
}
checkFileType() {
const res = this.fileName.split('.');
const fileType = res[res.length - 1];
if (fileType == 'pdf') {
this.isPdf = true;
} else if (fileType == 'docx') {
this.isDocx = true;
this.publicService.docx2html(this.fileName).subscribe(
response => {
this.html = response;
// console.log(response);
this.loading = false;
})
} else {
this.onError();
}
}
onRightClick() {
return false;
}
onLeftClick() {
return false;
}
afterLoading() {
this.loading = false;
}
onError() {
alert("Cannot load file");
}
}