我正在尝试在 Angular 11 项目中导入jschardet 。当前版本 3.0.0未在严格模式下运行(在运行时抛出 TypeError)。我试图在一个最小的例子中重现这个问题。因此,我生成了一个新的 Angular 项目并使用相同的依赖项和配置(即,我在两个项目中使用相同的tsconfig.json
、tsconfig.app.json
、和)。但是,TypeError 仅发生在我的大型/原始项目中。我未能在一个较小的项目中重现它。我猜我的大项目以某种方式以严格模式运行(即使我没有在提到的文件中明确指定它),但是我的新项目没有运行angular.json
package.json
package-lock.json
jschardet
jschardet
在严格模式下。我不知道,哪些文件与共享相关以解决这个问题。请在评论中告诉我。
无论如何,我不想在非严格模式下运行我的 Angular 应用程序,只有一个导入的库需要非严格模式。如何在非严格模式下导入/调用特定库?
以下文件、版本和更多信息:
- jscharder 3.0.0
- 打字稿 4.0.5
- @angular/编译器 11.1.0
src/app/app.component.ts
import { Component } from '@angular/core';
import * as jschardet from 'jschardet';
@Component({
selector: 'app-root',
template: `
<input type="file" id="file" (change)="decode($event)">
`
})
export class AppComponent {
decode(e: any) {
const file = e.target.files[0];
console.log(typeof file);
const fileReader = new FileReader();
fileReader.onload = function() {
const array = new Uint8Array(fileReader.result as ArrayBuffer);
let string = "";
for (let i = 0; i < array.length; ++i) {
string += String.fromCharCode(array[i]);
}
console.log(jschardet.detect(string));
};
fileReader.readAsArrayBuffer(file);
}
}
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"importHelpers": true,
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"downlevelIteration": true,
"target": "es2015",
"typeRoots": [
],
"lib": [
"es2017",
"dom",
"dom.iterable"
],
"types": [
"node",
"core-js"
],
"module": "es2020",
"baseUrl": "./"
}
}
src/tsconfig.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"baseUrl": "./",
"types": [ "node" ],
"typeRoots": [ "../node_modules/@types" ]
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
},
"files": [
"main.ts",
"polyfills.ts"
],
"include": [
"src/**/*.d.ts"
]
}
angular.json(缩短)
{
"projects": {
"my-app": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"aot": true,
"outputPath": "dist",
"index": "src/index.html",
"main": "src/main.ts",
"tsConfig": "src/tsconfig.app.json",
"polyfills": "src/polyfills.ts"
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "my-app:build"
}
}
}
}
}
}
我使用ng serve
.