【问题标题】:How to include Underscore.js in angular2 project built using angular-cli如何在使用 angular-cli 构建的 angular2 项目中包含 Underscore.js
【发布时间】:2017-09-19 19:46:52
【问题描述】:

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

1- npm install underscore --save

2- tsd 安装下划线

3- script 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”

【问题讨论】:

    标签: angular2-cli


    【解决方案1】:

    使用 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": ["../node_modules/underscore/underscore.js"]
    

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        Angular 2全3文件sn-ps

        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 install 4) 然后 npm 开始

        【讨论】:

          【解决方案4】:

          安装下划线:

          npm i underscore --save
          

          在 angular-cli.json 中:

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

          运行命令后:

          ng build
          

          在组件中:

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

          【讨论】:

            【解决方案5】:

            您的项目中有一个名为 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 。

            【讨论】:

            • 是的,package.json 有这个,但它没有更新 system-config.ts
            • 如果您需要更新 system-config.ts,您必须手动完成,就像您对 package.json 所做的那样。和 npm install,经过所有这些修改。
            • 我尝试了你的步骤,但是在运行 ng serve 时,它​​给出了错误“找不到模块下划线”。您能否为此提供一个有效的 plnkr。提前致谢
            【解决方案6】:

            我想你可能错过了一步?你还记得在“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 的孔路径希望这会有所帮助:)

            我和 moment 一样,来自文档:https://github.com/angular/angular-cli/wiki/3rd-party-libs

            【讨论】:

            • 我今天一定要试试这个。
            • 如果有什么需要详细说明的,请告诉我。
            • typings install underscore --save --global 给我这个错误“typings ERR! message Unable to find "underscore" ("npm") in the registry。”
            • 你不应该安装下划线全局。只需: npm install underscore --save
            • 再次出现这个错误 ::typings install underscore --save --global
            【解决方案7】:

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

            "underscore": "^1.8.3",
            

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

            它成功了……

            【讨论】:

              猜你喜欢
              • 2016-12-26
              • 1970-01-01
              • 2017-03-09
              • 1970-01-01
              • 1970-01-01
              • 2019-08-22
              • 1970-01-01
              • 1970-01-01
              • 2017-01-25
              相关资源
              最近更新 更多