我使用Angular CLI工具开始了一个新项目。之后,我按照this official guide导入下划线,然后按照说法完全 。
但是当我尝试在app.component中使用Underscore时,我的项目仍在浏览器中崩溃,并显示错误消息:
ORIGINAL EXCEPTION:ReferenceError:_未定义
下划线被添加到dist / vendor文件夹中,所以我的猜测是问题出在Systemjs配置中。
这是我的angular-cli-build:
var Angular2App = require('angular-cli/lib/broccoli/angular2-app');
module.exports = function(defaults) {
return new Angular2App(defaults, {
sassCompiler: {
includePaths: [
'src/styles'
]
},
vendorNpmFiles: [
'systemjs/dist/system-polyfills.js',
'systemjs/dist/system.src.js',
'zone.js/dist/**/*.+(js|js.map)',
'es6-shim/es6-shim.js',
'reflect-metadata/**/*.+(ts|js|js.map)',
'rxjs/**/*.+(js|js.map)',
'underscore/underscore.js',
'@angular/**/*.+(js|js.map)'
]
});
};
这是我的system-config:
"use strict";
// SystemJS configuration file, see links for more information
// https://github.com/systemjs/systemjs
// https://github.com/systemjs/systemjs/blob/master/docs/config-api.md
/***********************************************************************************************
* User Configuration.
**********************************************************************************************/
/** Map relative paths to URLs. */
const map: any = {
'underscore': 'vendor/underscore/',
};
/** User packages configuration. */
const packages: any = {
'underscore': { main: 'underscore.js', format: 'cjs' },
};
////////////////////////////////////////////////////////////////////////////////////////////////
/***********************************************************************************************
* Everything underneath this line is managed by the CLI.
**********************************************************************************************/
const barrels: string[] = [
// Angular specific barrels.
'@angular/core',
'@angular/common',
'@angular/compiler',
'@angular/forms',
'@angular/http',
'@angular/router',
'@angular/platform-browser',
'@angular/platform-browser-dynamic',
// Thirdparty barrels.
'rxjs',
// App specific barrels.
'app',
'app/shared',
/** @cli-barrel */
];
const cliSystemConfigPackages: any = {};
barrels.forEach((barrelName: string) => {
cliSystemConfigPackages[barrelName] = { main: 'index' };
});
/** Type declaration for ambient System. */
declare var System: any;
// Apply the CLI SystemJS configuration.
System.config({
map: {
'@angular': 'vendor/@angular',
'rxjs': 'vendor/rxjs',
'main': 'main.js'
},
packages: cliSystemConfigPackages
});
// Apply the user's configuration.
System.config({ map, packages });
我的app.component:
/// <reference path="../../typings/globals/underscore/index.d.ts" />
import { Component } from '@angular/core';
declare var _;
@Component({
moduleId: module.id,
selector: 'app-root',
template: `<h1>{{title}}</h1>`
})
export class AppComponent {
title = _.version;
}
我哪里出错了?
为什么这么复杂?社区是否会接受添加简单的第三方库这种麻烦?
答案 0 :(得分:3)
您的配置基本上设置了underscore
,因此SystemJS可以在需要时找到它。
当您更改 system-config.ts
时,您告诉SystemJS:如果有人要求underscore
,则可以在{{underscore.js
找到文件vendor/underscore/
1}}文件夹 - 其模块格式为CommonJS(cjs
)。
angular-cli-build.js
的更改是告诉angular-cli应该选择哪些文件并将其扔进vendor
文件夹。 (所以,如果你告诉SystemJS它会在那里找到下划线,这就是它存在的原因。)
但仅不会导入/添加下划线到您应用的全局范围内。
您必须在每个.ts
文件中导入它,以便SystemJS将其添加到该模块的已转换.js
。
而不是这两行:
/// <reference path="../../typings/globals/underscore/index.d.ts" />
declare var _;
将此内容添加到您的文件中:
import * as _ from 'underscore';
如果遇到问题,请尝试检查在浏览器中执行的生成的.js源。在您的情况下,您可能会发现没有require()
方法导入underscore
。
答案 1 :(得分:1)
添加第三方库的doco具有误导性。 我一直在敲我的头一个多小时!
if iter == '\n'
您需要的是
declare var _; // this won't work.