tôi thấy trong this post mà bạn có thể sử dụng SystemJS để tải các tập tin javascript bên ngoài vào thành phần của tôi trong góc 2.Làm thế nào tôi có thể sử dụng một system.import() vào phần góc 2
Trong index.html của tôi:
<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>
Và thành phần của tôi:
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();
});
}
}
Cuối cùng, khi tôi bắt đầu ứng dụng của mình:
frontOfficeA2/src/app/thành phần/main/main.ts (3,24): lỗi TS2307: Không thể tìm thấy mô-đun 'hệ thống'.
Nếu ai đó có một ý tưởng về những gì tôi làm sai .. :)
Cảm ơn :)
Bạn đã tải systemjs vào index.html của mình. Bạn không cần phải nhập nó. –