简体   繁体   中英

unable to use write-good npm module in angular 8

I am using Angular 8.2.4. I download the npm module write-good. When I try to use in my test component i am getting error ERROR ReferenceError: match is not defined. Here is the component code.

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.');
  }
}

Below is the error details,

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)

To achieve expected result , use below option of using default method of writeGood

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

working code for reference - https://stackblitz.com/edit/angular-1jkxhj?file=src/app/app.component.ts

Option 2: Use below line for importing write-good

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

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

Please refer this link for more details on import of javascript librairies with export of a function or class instead of modules or objects

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM