Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing code between Ionic and Angular application

I have a project where I have multiple Angular Apps, and an ionic application. I have a number of classes (models) and also services that can be shared between all the applications.

My first thought was to place the shared files into their own separate directory/folder and create a symbolic link. I have attempted to launch the ionic application and I'm receiving the following :

Module build failed: Error: /home/norman/Work/vem-shared/shared-services/table/table.service.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.

After searching for this error message I came across the following, however, this is for a lower angular version (I'm using Angular 6 and Ionic 4). I also noticed a number of posts about symbolic links not working within the angular cli - which appears to be for the older version.

I wanted to find out whether any knows how to resolve this error, and whether symbolic links work within an angular application/ionic application ?

Thanks

like image 281
user3836415 Avatar asked Aug 23 '18 05:08

user3836415


People also ask

Can Ionic be used with Angular?

Ionic supports Angular 6.0. 0 and up .

Can I add Ionic to existing Angular project?

For adding Ionic to an already existing Angular project, use the Angular CLI's ng add feature. This will add the necessary imports to the @ionic/angular package as well as add the styles needed.

Does Ionic use Angular or AngularJS?

Ionic uses the official Angular stack to ensure seamless integration with the existing Angular ecosystem. In short, the only change you'll notice with Angular Ionic in Ionic 4 is some improved performance.

Is Angular or Ionic better?

Option 3: Using Ionic you get the best of both worlds! You can create a normal web application just like angular but applying ionic specific styles, grid systems, buttons which of course you can modify at your wish. This same styles will automatically change for each platform, android, iOS, desktop browsers.


2 Answers

For Angular webapp the solution seems pretty straight forward by adding "preserveSymlinks": true to the angular.json file.

angular.json

{ 
    "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
    "version": 1,
    "newProjectRoot": "projects",
    "projects": {
        "my-project": {
            "root": "",
            "sourceRoot": "src",
            "projectType": "application",
            "architect": {
                "build": {
                    "builder": "@angular-devkit/build-angular:browser",
                    "options": {
                       "outputPath": "target",
                       "index": "src/index.html",
                       "main": "src/main.ts",
                       "tsConfig": "src/tsconfig.app.json",
                       "polyfills": "src/showcase/polyfills.ts",
                       "preserveSymlinks": true,
...

Now if you want to create library with preserved symlinks then you have to add the same feature in the tsconfig.lib.json because for library you don't have the angular.json

tsconfig.lib.json

"angularCompilerOptions": {
    "annotateForClosureCompiler": true,
    "skipTemplateCodegen": true,
    "strictMetadataEmit": true,
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
    "enableResourceInlining": true,
    "preserveSymlinks": true   },

For IONIC apps, symlink issue is a long pending one and it seems the solution is still in an open PR. You can track the progress here.

You can read more about the Angular webapp issue here

like image 196
argo Avatar answered Oct 21 '22 08:10

argo


You can create Angular Module with the shareable Services/Classes and put it onto NPM. Then install the module on all your applications and use it. You can update the module and keep updated on all your Application Code bases.

like image 30
Abdun Nahid Avatar answered Oct 21 '22 08:10

Abdun Nahid