10

我想在使用 angular-cli 构建的 angular2 项目中包含 underscore.js 。到目前为止,我无法这样做。到目前为止我尝试过:

1- npm 安装下划线 --save

2- tsd 安装下划线

3- 脚本 src="node_modules/underscore/underscore.js" ,参考 index.html

4- 内部 system-config.js

/** Map relative paths to URLs. */
var map = {
    'underscore': '../node_modules/underscore/underscore.js'
};
/** User packages configuration. */
var packages = {
    'underscore': '../node_modules/underscore/underscore.js'
};

5-从'下划线'导入*作为_;

但是 underscore.js 在运行时没有被复制到 'dist' 目录中,并且浏览器抱怨没有找到 underscore.js。我想我在第 4 点遗漏了一些东西。非常感谢任何帮助,因为我开始学习 angular2。请记住,这个项目是由 angular-cli 制作的,而不是由任何其他种子项目制作的。除了 Underscore.js,项目运行良好。

[编辑] package.json 在依赖项中有 "下划线": "^1.8.3"

4

7 回答 7

7

使用 Angular CLI 1.0.0-rc.1,推荐的解决方案如下所述:

Angular2 2.4 如何将外部库 sush 作为下划线加载到 angular2 中。

npm install underscore --save // save to dependencies: required to run
npm install @types/underscore --save-dev // save to dev dependencies: required in dev mode

然后在您的组件类中:

import * as _ from 'underscore';
...
subtitle = _.head(['xyz'])

或者,还有另一种方法可以在 angular-cli 中加载“静态”脚本,如此处所述https://www.npmjs.com/package/angular-cli#global-library-installation

在 .angular-cli.json 中,将其添加到脚本数组中:

"scripts": ["../node_modules/underscore/underscore.js"]

这将加载 underscore.js,但这不是使其可用于您的 typescript 类的好方法。

于 2017-03-17T17:31:00.967 回答
4

使用 npm 安装下划线转到 yourappname/src/typings.d.ts 并添加此行 declare module 'underscore'; 然后运行 ​​ng build

于 2016-12-13T13:00:04.070 回答
2

Angular 2 full 3 文件片段

包.json

    {
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
  },
  "license": "ISC",
  "dependencies": {
    "@angular/common": "2.0.0",
    "@angular/compiler": "2.0.0",
    "@angular/core": "2.0.0",
    "@angular/forms": "2.0.0",
    "@angular/http": "2.0.0",
    "@angular/platform-browser": "2.0.0",
    "@angular/platform-browser-dynamic": "2.0.0",
    "@angular/router": "3.0.0",
    "@angular/upgrade": "2.0.0",
    "angular2-in-memory-web-api": "0.0.20",
    "bootstrap": "^3.3.6",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.12",
    "systemjs": "0.19.27",
    "tsd": "^0.6.5",
    "underscore": "^1.8.3",
    "zone.js": "^0.6.23"
  },
  "devDependencies": {
    "concurrently": "^2.2.0",
    "lite-server": "^2.2.2",
    "typescript": "^2.0.2",
    "typings": "^1.3.2"
  }
}

systemjs.config.js

/**
 * System configuration for Angular 2 samples
 * Adjust as necessary for your application needs.
 */
(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',
      'underscore':                 'npm:underscore',

      'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },


      rxjs: {
        defaultExtension: 'js'
      },
      'underscore':{
       main: './underscore.js', 
       defaultExtension: 'js'
       },
      'angular2-in-memory-web-api': {
        main: './index.js',
        defaultExtension: 'js'
      }
    }
  });
})(this);

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  }
}

打字.json

{
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160725163759",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "node": "registry:dt/node#6.0.0+20160909174046"
  },
  "ambientDependencies": {
    "underscore": "github:DefinitelyTyped/DefinitelyTyped/underscore/underscore.d.ts#f0990e288ac73d593e982995947567aa2643b828"
  }
}

1- npm install underscore --save 2- tsd install underscore(如果 tsd 没有先安装那个) 3) 现在做 npm install 4) 然后 npm start

于 2016-09-18T17:11:16.017 回答
1

安装下划线:

npm i underscore --save

在 angular-cli.json 中:

"scripts": [
        "../node_modules/underscore/underscore-min.js",
         ...
      ],

运行命令后:

ng build

在组件中:

declare var _: any;
@Component({...})
于 2017-05-18T13:58:07.800 回答
0

我想你可能错过了一步?你还记得在“angular-cli-build.js”文件中设置下划线吗?这一步告诉 Clingon 在供应商文件夹 (/dist/vendor) 中添加下划线。

这是我如何得到下划线的工作。

  1. 安装下划线和打字:

    npm install underscore --save
    typings install underscore --save --ambient
    
  2. 在“angular-cli-build.js”中设置下划线:

    module.exports = function(defaults) {
      return new Angular2App(defaults, {
        vendorNpmFiles: [
          'systemjs/dist/system-polyfills.js',
          'systemjs/dist/system.src.js',
          'zone.js/dist/**/*.+(js|js.map)',
          'es6-shim/es6-shim.js',
          'reflect-metadata/**/*.+(js|js.map)',
          'rxjs/**/*.+(js|js.map)',
          '@angular/**/*.+(js|js.map)',
          'moment/moment.js',
          'underscore/underscore.js'
        ]
      });
    };
    
  3. 然后下划线被编译到供应商文件夹(/dist/vendor),现在可以从 system.config.ts 文件指向这个位置:

    const map: any = {
      "underscore": "vendor/underscore/underscore.js",
      "moment": "vendor/moment/moments.js"
    };
    
    /** User packages configuration. */
    const packages: any = {
      'underscore': {
        format: 'cjs'
      },
      'moment': {
        format: 'cjs'
      }
    };
    

请记住使用包含 .js 的孔路径希望这会有所帮助:)

我从文档中做了同样的事情:https ://github.com/angular/angular-cli/wiki/3rd-party-libs

于 2016-06-08T20:19:05.050 回答
0

我正在使用 Angular-cli,我所做的只是将这一行添加到package.json

"underscore": "^1.8.3",

然后我运行:
1. npm install underscore --save
2.npm install

它奏效了……

于 2017-03-04T13:05:17.707 回答
0

您的项目中有一个名为 Package.json 的文件吗?如果是,您可以尝试添加此行

"underscore": "^1.8.3",

在此文件的依赖项中。

system-config.ts 中的修改

var map = {

    "underscore": "node_modules/underscore",

  };

var packages = {
    'underscore':            { main: 'index.js', defaultExtension: 'js' }
  };

  var packageNames = [
    '@angular/common',
    '@angular/compiler',
    '@angular/core',
    '@angular/http',
    '@angular/platform-browser',
    '@angular/platform-browser-dynamic',
    '@angular/router-deprecated',
    '@angular/testing',
    '@angular/upgrade',
  ];

packageNames.forEach(function(pkgName) {
    packages[pkgName] = { main: 'index.js', defaultExtension: 'js' };
  });

var config = {
    map: map,
    packages: packages,
    paths: {
      "underscore": "/node_modules/underscore.js"
    }
  };

并在那之后进行 npm install 。

于 2016-06-04T12:45:27.457 回答