Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I use a system.import() into component angular 2

I saw in this post that you can use SystemJS to load external javascript files into my components in Angular 2.

In my index.html :

<script>
        System.config({
            packages: {
                "frontOfficeA2/src": {
                    format: 'register',
                    defaultExtension: 'js'
                },
                "angular2-jwt": {
                    "defaultExtension": "js"
                },
                "ng2-bootstrap": {
                    "defaultExtension": "js"
                },
                "system": {
                    "defaultExtension": "js"
                }
            },
            map: {
                "angular2-jwt": "lib/angular2-jwt",
                "ng2-bootstrap": "lib/ng2-bootstrap",
                "moment": 'lib/moment/moment.js',
                "system": 'lib/systemjs/dist/system.src.js'
            }
        });
        System.import('frontOfficeA2/src/app.js').then(null, console.error.bind(console));
    </script>

And my component :

import {Component} from 'angular2/core';
import { DATEPICKER_DIRECTIVES } from 'ng2-bootstrap/ng2-bootstrap';
import { System } from 'system';

@Component({
  selector: 'main',
  templateUrl: 'app/components/main/main.html',
  styleUrls: ['app/components/main/main.css'],
  providers: [],
  directives: [DATEPICKER_DIRECTIVES],
  pipes: []
})
export class Main {
    date: Date = new Date();
    constructor() {
        System.import('path/to/your/file').then(refToLoadedScript => {
            refToLoadedScript.someFunction();
        });
    }
}

Finally, when I start my app :

frontOfficeA2/src/app/components/main/main.ts(3,24): error TS2307: Cannot find module 'system'.

If somebody have an idea of what am I doing wrong .. :)

Thanks :)

like image 519
Mathieu Allain Avatar asked Mar 09 '16 17:03

Mathieu Allain


People also ask

Can we import one component to another component in Angular?

Yes , It possible to include child component by using selector as class name. This way will require some changes in selector metadata of the component.

What is the use of import and export in Angular?

An import is what you put in the imports property of the @NgModule decorator. It enables an Angular module to use functionality that was defined in another Angular module. An export what you put is the exports property of the @NgModule decorator.

What is import statement in Angular?

Or Simply, to use any methods defined in other classes, we need to import it first.. So its understandable. And also in the NgModule decorative, in the import statement, we import them.. Because an angular module (as a feature) defines what are the modules that can be used by its components, services etc..

Can two modules import each other in Angular?

Sharing moduleslinkYou can put commonly used directives, pipes, and components into one module and then import just that module wherever you need it in other parts of your application.


1 Answers

You can use systemjs to do your external dependency loading.

npm i systemjs --save-dev
npm i @types/systemjs --save-dev

You'll need to update your tsconfig.app.json file (tsconfig.json file for older versions of Angular).

"types": ["systemjs"]

Now you'll be able to import

System.import("your-url").then(response => response.methodCall());

If you have an import map specified

<script type="systemjs-importmap">
 {
  "imports": {
    "import-name": "external-code-url",
  }
 }
</script>

You can instead call this code

System.import("import-name").then(response => responce.methodCall());
like image 195
Luminous Avatar answered Oct 06 '22 13:10

Luminous