Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i export a module from one angular2-cli project to another?

Tags:

angular

I want to copy a singular madule from on project to another without hard copy. and need a dependency manager like npm to handle this problem. my two project develop in one company & i can clone two project in on pc.

i want to export a module from one project and import this module to another project with typescript export&import.

please help me to resolve this problem.

like image 408
hamid_reza hobab Avatar asked May 05 '18 09:05

hamid_reza hobab


People also ask

How to publish a library in Angular 2?

in order to publish an angular library first of all you should create your own public API module ( project 1 ), and with a packager (like ng-Packagr) pack the library. and finally use your library in other projects (project 2) in the @NgModule decorator, identify the export components, in export array section, like below example:

What is the difference between ES module export and angular export?

ES module export and angular export are two different things. What I propose is just ES module export which means you just export the class itself. There is nothing angular related going on in this line. Also, OP should not put MyApi in exports array btw. Angular modules do not exports services, just directives/pipes/modules.

How to add all files to a component in angular?

As all the files are located under the product-list directory, the simplest way is to add all files in the directory to your component. Bit will name it accordingly. For Angular components, we also need to specify the component entry point, which in most cases will be the file containing the ngModule.

How to create module with routing and specs in angular?

For example, to create module with routing and specs use the following command. $ ng g m guest -sp -r installing module create src/app/guest/guest-routing.module.ts create src/app/guest/guest.module.spec.ts create src/app/guest/guest.module.ts All generate module flags: PDF - Download angular-cli for free


Video Answer


2 Answers

in order to publish an angular library first of all you should create your own public API module (project 1), and with a packager (like ng-Packagr) pack the library. and finally use your library in other projects (project 2)

project 1:

  1. in the @NgModule decorator, identify the export components, in export array section, like below example:

exports: [ReviewComponent, ]

  1. and modify package.json file. identity packager in script section:

    "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e", **"packagr": "ng-packagr -p ng-package.json"** }

  2. create ng-package.json config:

    // here is an example { "$schema": "./node_modules/ng-packagr/ng-package.schema.json", "lib": { "entryFile": "public_api.ts",} }

  3. create public_api.ts config. that say's which module should be packed for export purpose:

export * from './src/app/reg-review/reg-review.module'

  1. run the following command:

npm run packagr

  • after your package is created go to the dist subfolder and run this command: npm pack

project 2:

  • refrence to the library( which were build in dist subfolder of project 1)npm install ../ngLibs/reg/registeration-0.0.0.tgz --save
    • import your desigerd module and use it import { RegisterReviewModule } from 'registeration'

you can take advantage of these addresses:

https://www.npmjs.com/package/ng-packagr

https://medium.com/@nikolasleblanc/building-an-angular-4-component-library-with-the-angular-cli-and-ng-packagr-53b2ade0701e

like image 139
Morteza Mahloujian Avatar answered Nov 03 '22 00:11

Morteza Mahloujian


There are multiple options here, depending on what you want to achieve you might pick one over another:

  • there can be multiple apps within the same CLI project, the apps property in the config file is an array
  • one can use tools like ng-packagr to extract and publish external modules
  • one can use Nx to manage all apps and libraries in a monorepo like approach
  • could use yarn workspaces to achieve a similar monorepo approach
  • if you want to share components look at tools like Bitsrc
  • etc.

In the end it's about what requirements you have and how you want to work within your company.

like image 39
Adrian Fâciu Avatar answered Nov 03 '22 00:11

Adrian Fâciu