简体   繁体   English

如何在 Angular(2/4) 中导入“url-regex”npm 包?

[英]How to import 'url-regex' npm package in Angular(2/4)?

I use Angular 4 and have trouple using url-regex package in my Component.我使用 Angular 4 并且在我的组件中使用url-regex包有麻烦。

In some cases, I have found out that this works:在某些情况下,我发现这有效:

import * as urlRegex from 'url-regex';

But does not work in my case, inside constructor() and ngOnInit() , the value of urlRegex is undefined .但在我的情况下不起作用,在constructor()ngOnInit()urlRegex的值是undefined

This is the index.js file from this package这是这个包中的index.js文件

Can someone help me?有人可以帮助我吗?

After import, if I call urlRegex() anywhere inside the Component it works!导入后,如果我在组件内的任何地方调用urlRegex() ,它就会起作用!

At debugging in the Browser, when I hover over urlIndex() it shows (anonymous) ;在浏览器中调试时,当我将鼠标悬停在urlIndex()它会显示(anonymous) 在此处输入图片说明

Probably it has to do with the import of Webpack.可能跟引入 Webpack 有关系。

There's a default export (you can see this if you use VScode or a similar editor), so rather than importing everything with import * , just use:有一个默认导出(如果您使用 VScode 或类似的编辑器,您可以看到这一点),因此不要使用import *导入所有内容,只需使用:

import urlRegex from "url-regex"

Then to produce the RegExp, run urlRegex()然后生成 RegExp,运行urlRegex()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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