简体   繁体   中英

How to import npm packages in angular?

Im trying to import a package called cssdom in angular:

like this:

import * as CssDom from "cssdom";

But I'm getting the following error:

在此处输入图像描述

when I try to create a new instance of CssDom like this:

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

I tried this way also:

import {CssDom} from 'cssdom'

Or is there a way to import these third party packages in to angular?

In vue.js this is very easy. As you can import simply. But with typescript/angular this seems hard.!

First install cssdom (if not done yet):

$ npm i cssdom

Solution 1:

Add the script to angular.json

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

Next use cssdom like this:

declare const CssDom:any;

...

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

Solution 2:

declare var CssDom:any;

import "cssdom";

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