带有webpack的Angular 2获取Uncaught ReferenceError:未定义系统

时间:2016-10-14 08:01:49

标签: testing angular webpack karma-jasmine referenceerror

我有一个工作的Angular 2项目,我试图添加一些测试。现在通过测试我添加了webpack,现在有一些问题,我无法通过在线研究解决。这是我在浏览器中得到的错误消息:
未捕获的ReferenceError:系统未在localhost /:8中定义 未捕获的ReferenceError:系统未在systemjs.config.js中定义:42

在这里我的结构:
http://imgur.com/a/dMVfp
这是我的index.html:

<html>
<head>
    <title>PQM Monitor</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="src/css/styles.css">
    <script>
        System.import('/src/app').catch(function(err){console.error(err);});
    </script>
    <script src="systemjs.config.js"></script>
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/ng2-bs3-modal/bundles/ng2-bs3-modal.js"></script>
    <script src="node_modules/chart.js/dist/Chart.bundle.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
    <link href="src/css/bootstrap.min.css" rel="stylesheet">
    <link href="src/images/favicon.png" rel="icon" type="image"/>
    <base href="/src">
</head>
<body>
<info>Loading...</info>
<script src="src/js/jquery.min.js"></script>
<script src="src/js/bootstrap.min.js"></script>
</body>
</html>

这是我的systemjs.config.js:

/**
 * System configuration for Angular 2 samples
 * Adjust as necessary for your application needs.
 */
(function(global) {
    // map tells the System loader where to look for things
    var map = {
        'app':                          'dist', //'src/app',
        '@angular':                     'node_modules/@angular',
        'angular2-in-memory-web-api':   'node_modules/angular2-in-memory-web-api',
        'rxjs':                         'node_modules/rxjs',
        'polyfill':                     'node_modules/babel-polyfill',
    };
    // packages tells the System loader how to load when no filename and/or no extension
    var packages = {
        'app':                          { main: 'main.js',  defaultExtension: 'js' },
        'rxjs':                         { defaultExtension: 'js' },
        'angular2-in-memory-web-api':   { main: 'index.js', defaultExtension: 'js' },
    };
    // packages tells the System loader how to load when no filename and/or no extension
    var ngPackageNames = [
        'common',
        'compiler',
        'core',
        'forms',
        'http',
        'platform-browser',
        'platform-browser-dynamic',
        'router',
        'router-deprecated',
        'upgrade',
    ];
    // Individual files (~300 requests):
    function packIndex(pkgName) {
        packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
    }
    // Bundled (~40 requests):
    function packUmd(pkgName) {
        packages['@angular/'+pkgName] = { main: 'bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
    }
    // Most environments should use UMD; some (Karma) need the individual index files
    var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
    // Add package entries for angular packages
    ngPackageNames.forEach(setPackageConfig);
    var config = {
        map: map,
        packages: packages
    };
    System.config(config);
})(this);


1 个答案:

答案 0 :(得分:1)

您在导入之前调用系统,只需更改系统导入位置,以确保在导入system.src.js文件后调用它:

<html>
<head>
    <title>PQM Monitor</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="src/css/styles.css">
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/ng2-bs3-modal/bundles/ng2-bs3-modal.js"></script>
    <script src="node_modules/chart.js/dist/Chart.bundle.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
    <script src="systemjs.config.js"></script>
    <link href="src/css/bootstrap.min.css" rel="stylesheet">
    <link href="src/images/favicon.png" rel="icon" type="image"/>
    <base href="/src">
</head>
<body>
<script>
    System.import('/src/app').catch(function(err){console.error(err);});
</script>
<info>Loading...</info>
<script src="src/js/jquery.min.js"></script>
<script src="src/js/bootstrap.min.js"></script>
</body>
</html>