问题描述: 在Angular 7中,使用pdf-viewer插件无法显示pdf文件。
解决方法:
npm install ng2-pdf-viewer --save
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
// 导入pdf-viewer模块
import { PdfViewerModule } from 'ng2-pdf-viewer';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
// 添加PdfViewerModule到导入模块中
PdfViewerModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
在组件的TS文件中,定义pdfSrc变量并指定pdf文件的URL或Base64数据:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
// 指定pdf文件的URL或Base64数据
pdfSrc: string = 'https://example.com/example.pdf';
}
如果仍然无法显示pdf文件,可以检查网络连接和pdf文件的有效性。