Angular CLIツールを使用して新しいプロジェクトを開始しました。その後、アンダースコアをインポートするにはthis official guideに従います。と言いますと、ちょうどです。ブラウザでアンダースコアをAngular 2 CLIプロジェクトにインポート
しかし、まだ私のプロジェクトがクラッシュし、私はエラーメッセージを表示して、私のapp.componentにアンダースコアを使用しよう:下線がDIST /ベンダーフォルダに追加され
ORIGINAL EXCEPTION: ReferenceError: _ is not defined
ので、私の推測では、ということでしょう問題はSystemjsの設定にあります。ここで
は私の角度-CLI-ビルドです:ここで
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)'
]
});
};
は私のシステム-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;
}
違う?
およびWHYはこれほど複雑ですか?単純なサードパーティのライブラリを追加するのは面倒です。
あり、それは動作しません。この作業を行うためにsystem.jsの設定をどのようにする必要がありますか? – mottosson
私はアンダースコアを本当に知っていません。本当に各libに依存しますが、あなたのコードは正しいと思われます。アンダースコアのどのバージョンを使用していますか? devまたはプロダクションの依存関係としてインポートしていますか? – acdcjunior
私はバージョン1.8.3を使用し、それを通常の依存関係に追加しました。 ライブラリは私のdist/vendorフォルダに追加されているので、angle-cli-buildは正しいと思いますし、問題はsystemjsの設定です。 – mottosson