如何在使用angular-cli

时间:2016-06-04 12:16:06

标签: angular2-cli

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

1- npm install underscore --save

2- tsd安装下划线

3- script src =" node_modules / underscore / underscore.js" ,在index.html中引用

4- inside 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- import * as _ from' underscore';

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

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

7 个答案:

答案 0 :(得分:7)

使用Angular CLI 1.0.0-rc.1,此处描述了推荐的解决方案:

Angular2 2.4 How to load external libraries sush as Underscore into 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数组中:

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

这将加载underscore.js,但这不是一个让它可用于你的打字稿类的好方法。

答案 1 :(得分:4)

使用npm安装下划线 转到yourappname / src / typings.d.ts并添加此行 RewriteEngine On # redirect home page to http:// RewriteCond %{HTTPS} on RewriteRule ^/?$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] # redirect anything but home page to https:// RewriteCond %{HTTPS} off RewriteCond %{THE_REQUEST} !^[A-Z]{3,}\s/+[\s?] [NC] RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] # rest of your rules go here 然后运行ng build

答案 2 :(得分:2)

Angular 2完整3个文件片段

的package.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
  }
}

typings.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安装下划线--save 2- tsd安装下划线(如果tsd没有安装第一次安装那个) 3)现在做npm安装 4)然后npm开始

答案 3 :(得分:1)

安装下划线:

npm i underscore --save

在angular-cli.json中:

"scripts": [
        "../node_modules/underscore/underscore-min.js",
         ...
      ],
运行命令后

ng build
组件中的

declare var _: any;
@Component({...})

答案 4 :(得分: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安装。

答案 5 :(得分: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'
      }
    };
    
  4. 请记住使用包含.js的孔路径,希望这有助于:)

    我从文档:https://github.com/angular/angular-cli/wiki/3rd-party-libs

    做了同样的事

答案 6 :(得分:0)

我使用的是Angular-cli,我所做的就是在package.json中添加这一行

"underscore": "^1.8.3",

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

它有效......