【问题标题】:unable to use write-good npm module in angular 8无法在 Angular 8 中使用 write-good npm 模块
【发布时间】:2020-01-11 13:06:41
【问题描述】:

我正在使用 Angular 8.2.4。我下载了 npm 模块 write-good。当我尝试在我的测试组件中使用时,我收到错误 ERROR ReferenceError: match is not defined。 这是组件代码。

import { Component, OnInit } from '@angular/core';
import * as  writeGood from 'write-good';

@Component({
  selector: 'app-textnpm',
  templateUrl: './textnpm.component.html',
  styleUrls: ['./textnpm.component.css']
})
export class TextnpmComponent implements OnInit {
  testString: string;
  constructor() { }

  ngOnInit() {
    this.hello();
  }

  public hello() {
    this.testString = writeGood('so so the cat was stolen.');
  }
}

以下是错误详情,

ERROR ReferenceError: match is not defined
    at Object.push../node_modules/weasel-words/weasel.js.module.exports [as fn] (weasel.js:40)
    at write-good.js:101
    at Array.forEach (<anonymous>)
    at writeGood (write-good.js:97)
    at TextnpmComponent.hello (textnpm.component.ts:18)
    at TextnpmComponent.ngOnInit (textnpm.component.ts:14)
    at checkAndUpdateDirectiveInline (core.js:31909)
    at checkAndUpdateNodeInline (core.js:44366)
    at checkAndUpdateNode (core.js:44305)
    at debugCheckAndUpdateNode (core.js:45327)

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    为了达到预期的效果,请使用下面使用 writeGood 的默认方法的选项

    public hello() {
        this.testString = writeGood.default('so so the cat was stolen.');
        console.log(this.testString);
      }
    

    工作代码供参考 - https://stackblitz.com/edit/angular-1jkxhj?file=src/app/app.component.ts

    选项 2: 使用下面的行来导入 write-good

    import writeGood = require('write-good');

    https://stackblitz.com/edit/angular-eaf4o9?file=src/app/app.component.ts

    请参阅此链接以获取有关通过导出函数或类而不是模块或对象来导入 javascript 库的更多详细信息

    Difference between `import from` and `import require` in TypeScript

    【讨论】:

    • 同样的错误。即使没有默认方法,它也可以在 stackblitz 上运行。
    • @Waqas,用选项 2 和参考链接更新了我的答案
    • 编译时出现以下错误 ----- 错误 TS1202:以 ECMAScript 模块为目标时无法使用导入分配。考虑使用 'import * as ns from "mod"'、'import {a} from "mod"'、'import d from "mod"' 或其他模块格式。
    猜你喜欢
    • 2016-03-23
    • 2016-09-07
    • 2017-01-19
    • 1970-01-01
    • 2020-01-02
    • 2015-11-15
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    相关资源
    最近更新 更多