简体   繁体   中英

Angular 2, import custom package

I'm using angular 2+, I have copy a custom package into my node_module directory like this node_module/@customPackage/forms so in my component code I import it like this import {blabla}from '@customPackage/forms'. but at the compilation time I still have an error saying "[ts] cannot find module @customPackage/forms".

I really don't know why I have that error. Please help. Thanks

您可以在package.json文件中的dependencies数组中写下包名称,然后点击npm install .so 安装所有依赖项

Hi no you dont need to copy paste your package, The correct way to use your custom package into the angular application, is to link it like so:

STEPS :
1. inside your custom package folder:

  • yarn build

  • yarn link :

  • this will display something like this(this is from my custom package) .

    • see the yarn link "my-package ":

在此处输入图片说明

  1. Now, go into your angular project folder and type the above sentence, to link your custom package into your angular project:
    • yarn link "my-package" (we need the double quotes around the package name) 在此处输入图片说明
  2. open your code editor, if you use vscode you should see the package into node_modules folder, with a small arrow on the right like so: 在此处输入图片说明

  3. If you succeed on the above, you can now use your module like eg import { mymodule } from 'my-package

Hope this helps.

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