首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在angular2中导入外部库?

如何在angular2中导入外部库?
EN

Stack Overflow用户
提问于 2017-02-03 01:54:23
回答 1查看 1.1K关注 0票数 1

我正在遵循这个(https://www.thepolyglotdeveloper.com/2016/01/include-external-javascript-libraries-in-an-angular-2-typescript-project/)来导入外部库。然而,我不能以同样的方式模仿它。我得到了以下错误,

获取http://127.0.0.1:8080/src/rxjs/Subject 404 (未找到)。

在src文件夹中,我有两个文件夹,app和js,以及两个文件,index.html和tsconfig.json。

App.ts

代码语言:javascript
代码运行次数:0
运行
复制
declare var jsSHA: any;

@Component({
    selector: "my-app",
    templateUrl: "app/app.html",
    directives: []
})

class App {

    shaObj: any;
    hash: String;

    constructor() {
        this.shaObj = new jsSHA("SHA-512", "TEXT");
        this.shaObj.update("This is a test");
        this.hash = this.shaObj.getHash("HEX");
    }

}

bootstrap(App, []);

app.html

代码语言:javascript
代码运行次数:0
运行
复制
<h1>SHA-512 Hash</h1>

<p>String: This is a test</p>
<p>HEX: {{hash}}</p>

Index.html

代码语言:javascript
代码运行次数:0
运行
复制
<html>
    <head>
        <title>Angular 2 QuickStart</title>
        <script src="../node_modules/angular2/bundles/angular2-polyfills.js"></script>
        <script src="../node_modules/systemjs/dist/system.src.js"></script>
        <script src="../node_modules/rxjs/bundles/Rx.js"></script>
        <script src="../node_modules/angular2/bundles/angular2.dev.js"></script>
        <script src="js/sha.js"></script>
        <script>
            System.config({
                packages: {'app': {defaultExtension: 'js'}}
            });
            System.import('app/app');
        </script>
    </head>
    <body>
        <my-app>Loading...</my-app>
    </body>
</html>

system.config.js

代码语言:javascript
代码运行次数:0
运行
复制
(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'npm:': 'node_modules/'
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            app: 'app',
            // angular bundles
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
            // other libraries
            'rxjs': 'npm:rxjs',
            'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
            'underscore': 'node_modules/underscore/underscore.js'
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './app.js',
                defaultExtension: 'js'
            },
            rxjs: {
                defaultExtension: 'js'
            }
        }
    });
})(this);

如果有人对这个问题有任何线索,请给我建议。

EN

回答 1

Stack Overflow用户

发布于 2017-11-20 03:34:09

如果您的sha.js是您自己编写的模块,则应添加sha.d.ts文件以便将其导入您的应用程序:guide fo this

如果是外部库,请将其作为常规npm包安装:

代码语言:javascript
代码运行次数:0
运行
复制
npm install —save sha
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42009111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档