【问题标题】:How to import npm packages in angular?如何以角度导入 npm 包?
【发布时间】:2021-02-08 03:34:55
【问题描述】:

我正在尝试以角度导入一个名为 cssdom 的包:

像这样:

import * as CssDom from "cssdom";

但我收到以下错误:

当我尝试像这样创建一个新的 CssDom 实例时:

const css = '.container { background-color: white; }';
const cssdom = new CssDom(css)

我也试过这种方式:

import {CssDom} from 'cssdom'

或者有没有办法将这些第三方包导入到 Angular 中?

在 vue-js 中这很容易。因为您可以简单地导入。但是对于 typescript/angular,这似乎很难。!

【问题讨论】:

    标签: angular typescript npm


    【解决方案1】:

    首先安装 cssdom(如果尚未完成):

    $ npm i cssdom
    

    解决方案 1:

    将脚本添加到angular.json

      "scripts": [
         ...
        "../node_modules/cssdom/cssdom.js"
         ...
    ],
    

    接下来像这样使用cssdom:

    declare const CssDom:any;
    
    ...
    
    const css = '.container { background-color: white; }';
    const cssdom = new CssDom(css)
    

    解决方案 2:

    declare var CssDom:any;
    
    import "cssdom";
    

    【讨论】:

    • 是否需要将css dom导入到组件中?
    • 我收到此错误:scripts.js:2706 Uncaught ReferenceError: require is not define。这是来自上述包的脚本!!
    • 除此之外我也收到此错误:TypeError: Scanner is not a constructor at new CssDom
    • 你可以试试我的第二个解决方案
    • 新错误 Uncaught (in promise): ReferenceError: process is not defined ReferenceError: process is not defined 以及 Uncaught ReferenceError: require is not defined
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2021-12-15
    • 2019-03-05
    • 2021-01-06
    • 2022-01-24
    • 2020-03-06
    • 1970-01-01
    相关资源
    最近更新 更多