Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using external javascript AMD Modules in Angular2 CLI

When I was using Angular2 pre-releases I had to use systemjs in order to use external javascript libraries, namely ESRI ArcGIS JavaScript API, which is based on AMD modules (typings are available though).

Now I would like to migrate to Angular2 CLI. However, I fail to see how I can import an external library like arcgis after Angular2 moved from systemjs to webpack and without having a webpack.config file to modify.

The external library is stored under:

<script src="https://js.arcgis.com/3.19/"></script>

Example: In my angular components, I somehow need to be able to import these like:

import map from 'esri/map';

to import 'https://js.arcgis.com/3.19/esri/map.js'

Any idea how this can be accomplished?

Edit: If I add the link to the map.js to the angular-cli.json

"scripts": [
          "../node_modules/jquery/dist/jquery.js",
           ....
          "https://js.arcgis.com/3.19/esri/map.js"
      ],

and import it as import {map} from 'esri/map';

I'm getting a webpackMissingModule: C:\Users\xy\project\src\https:\js.arcgis.com\3.19\esri\map.j‌​s

like image 655
netik Avatar asked Feb 01 '17 10:02

netik


2 Answers

the sample angular-cli.json for including external libraries sorry for dump data but this is a hint as you can see i added scripts and css in styles and scripts and addons array in angular-cli.json

{
  "project": {
    "version": "1.0.0-beta.19-3",
    "name": "adm"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "mobile": false,
      "styles": [
        "assets/lib/bootstrap/css/bootstrap.css",
        "../node_modules/font-awesome/css/font-awesome.css",
        "assets/lib/metismenu/metisMenu.css",
        "assets/lib/animate.css/animate.css",
        "assets/css/customradiocss.css",
        "rainbow/blackboard.css"
      ],
      "scripts": [
          "assets/lib/jquery/jquery.js",
          "assets/lib/bootstrap/js/bootstrap.js",
          "assets/lib/metismenu/metisMenu.js",
          "assets/lib/screenfull/screenfull.js",
          "assets/js/core.js",
          "assets/js/app.js",
          "assets/js/easyResponsiveTabs.js",
          "rainbow/rainbow-custom.min.js"
      ],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": ["../node_modules/font-awesome/fonts/*.+(otf|eot|svg|ttf|woff|woff2)"],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false,
    "inline": {
      "style": false,
      "template": false
    },
    "spec": {
      "class": false,
      "component": true,
      "directive": true,
      "module": false,
      "pipe": true,
      "service": true
    }
  }
}
like image 53
anshuVersatile Avatar answered Oct 15 '22 10:10

anshuVersatile


You can do this in one of two ways:

  1. Import the script inside your index.html, then in your component, declare the API entry point as a constant:

    const argGIS: any;

Then use it as any other place you have used it. You will have no intelli-sense support but it will work.

  1. Use a npm module, it looks like one exists. Install it with:

    npm install arcgis --save

Add it to your "scripts" in angular-cli.json

"../node_modules/arcgis/file.js"

Look at the index.d.ts file to find what to import and import it in your component with:

import {Something} from 'arcgis';
like image 23
Ben Richards Avatar answered Oct 15 '22 10:10

Ben Richards