[英]FileReader is not defined with Angular Server Side Rendering
I'm setting Server Side Rendering in a large webapp.我正在一个大型 web 应用程序中设置服务器端渲染。 It works well for page without FileReader instance.它适用于没有 FileReader 实例的页面。 Do you know how can I make FileReader to work with SSR enable?你知道如何让 FileReader 与 SSR 一起工作吗?
I found an NPM package for filereader.我找到了一个用于文件阅读器的 NPM 包。 I installed and import it in server.ts import * as FileReader from 'filereader';我在 server.ts 中安装并导入它 import * as FileReader from 'filereader'; but I got no result from that.但我没有得到任何结果。
I also added the filereader package in the whitelist of webpack.server.config.ts with no luck again.我还在 webpack.server.config.ts 的白名单中添加了 filereader 包,但没有成功。
I tried to replace FileReader with new Response(blob).text();我试图用 new Response(blob).text(); 替换 FileReader; but in that case it is the Response which is undefined too.但在这种情况下,响应也是未定义的。
I also tried window.FileReader() and global.FileReader() and no luck again.我也试过 window.FileReader() 和 global.FileReader() ,但又没有运气。
This is the method with FileReader:这是 FileReader 的方法:
function blobToText(blob: any): Observable<string> {
return new Observable<string>((observer: any) => {
if (!blob) {
observer.next("");
observer.complete();
} else {
let reader = new FileReader(); // FileReader is not defined
reader.onload = event => {
observer.next((<any>event.target).result);
observer.complete();
};
reader.readAsText(blob);
}
});
}
Here is the error message这是错误信息
I got: ERROR { Error: Uncaught (in promise): ReferenceError: FileReader is not defined ReferenceError: FileReader is not defined at Observable._subscribe....我得到:错误 { 错误:未捕获(承诺):ReferenceError:FileReader 未定义 ReferenceError:FileReader 未在 Observable._subscribe 处定义...。
function blobToText(blob: any): Observable<string> {
return new Observable<string>((observer: any) => {
if (!blob) {
observer.next("");
observer.complete();
} else {
if (typeof window === 'undefined') {
observer.next(blob);
observer.complete();
} else {
let reader = new FileReader();
reader.onload = event => {
observer.next((<any>event.target).result);
observer.complete();
};
reader.readAsText(blob);
}
}
});
}
You need to install "@types/node"您需要安装“@types/node”
By this command通过这个命令
npm install --save @types/node
Then in your tsconfig.json add然后在你的 tsconfig.json 添加
"typeRoots": [
"node_modules/@types"
]
If not work open your tsconfig.app.json file add to types section如果不起作用,请打开您的 tsconfig.app.json 文件添加到类型部分
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"module": "esNext",
"types": ["node"] // add node here
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
Maybe this answer comes too late but in case anybody else could run into this issue, this solution worked for me, when using Angular 9 .也许这个答案来得太晚了,但万一其他人可能遇到这个问题,这个解决方案对我有用,当使用Angular 9 时。 So basically you need to create a block of code that only gets executed when your application is on the browser not in the server side:所以基本上你需要创建一个代码块,只有当你的应用程序在浏览器上而不是在服务器端时才会被执行:
//Import PLATFORM_ID and Inject from @angular/core
import { Component, OnInit, PLATFORM_ID, Inject } from '@angular/core';
@Component({
selector: 'app-new-post',
templateUrl: './new-post.component.html',
styleUrls: ['./new-post.component.scss']
})
export class NewPostComponent implements OnInit {
//Change the FileReader variable
reader; //before it was: reader: FileReader = new FileReader();
//Inject PLATFORM_ID as follow:
constructor( @Inject(PLATFORM_ID) private platformId: any,
private readonly activatedRoute: ActivatedRoute,
private readonly fileUploadService: FileUploadService) {
}
ngOnInit() {
//Create a code block that only gets executed when it's loaded in the browser:
if (isPlatformBrowser(this.platformId)) {
this.reader = new FileReader();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.