gpt4 book ai didi

angularjs - 使用 lodash 构建 Angular 2 CLI

转载 作者:太空狗 更新时间:2023-10-29 19:31:00 26 4
gpt4 key购买 nike

导入总是导致错误“找不到模块 lodash”

import _ from 'lodash';

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/**/*.+(ts|js|js.map)',
'rxjs/**/*.+(js|js.map)',
'@ngrx/**/*.+(js|js.map)',
'@angular/**/*.+(js|js.map)',
'@angular2-material/**/*.js',
'angularfire2/**/*.js',
'firebase/*.js',
'lodash/**/*.js'
]
});
};

系统配置.js

// 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 = {
'@angular2-material': 'vendor/@angular2-material',
'@ngrx': 'vendor/@ngrx',
'firebase': 'vendor/firebase/firebase.js',
'angularfire2': 'vendor/angularfire2',
'lodash': 'vendor/lodash'
};

/** User packages configuration. */
const packages: any = {
'@angular2-material/button': {
format: 'cjs',
defaultExtension: 'js',
main: 'button.js'
},
'@angular2-material/card': {
format: 'cjs',
defaultExtension: 'js',
main: 'card.js'
},
'@angular2-material/core': {
format: 'cjs',
defaultExtension: 'js',
main: 'core.js'
},
'@angular2-material/checkbox': {
format: 'cjs',
defaultExtension: 'js',
main: 'checkbox.js'
},
'@angular2-material/icon': {
format: 'cjs',
defaultExtension: 'js',
main: 'checkbox.js'
},
'@angular2-material/input': {
format: 'cjs',
defaultExtension: 'js',
main: 'input.js'
},
'@angular2-material/list': {
format: 'cjs',
defaultExtension: 'js',
main: 'list.js'
},
'@angular2-material/sidenav': {
format: 'cjs',
defaultExtension: 'js',
main: 'sidenav.js'
},
'@angular2-material/toolbar': {
format: 'cjs',
defaultExtension: 'js',
main: 'toolbar.js'
},
'@ngrx/core': {
main: 'index.js',
format: 'cjs'
},
'@ngrx/store': {
main: 'index.js',
format: 'cjs'
},
'angularfire2': {
defaultExtension: 'js',
main: 'angularfire2.js'
},
'lodash': {
main:'index.js',
defaultExtension:'js'
}
};

////////////////////////////////////////////////////////////////////////////////////////////////
/***********************************************************************************************
* Everything underneath this line is managed by the CLI.
**********************************************************************************************/
const barrels: string[] = [
// Angular specific barrels.
'@angular/core',
'@angular/common',
'@angular/compiler',
'@angular/http',
'@angular/forms',
'@angular/router',
'@angular/platform-browser',
'@angular/platform-browser-dynamic',

// Thirdparty barrels.
'rxjs',
'lodash',

// App specific barrels.
'app',
'app/services',
'app/reducers',
'app/dashboard',
'app/http-start',
'app/http-finish',
'app/wikipedia-search-start',
'app/wikipedia-search-finish',
'app/todo-list',
'app/todo-list-firebase',
'app/todos',
'app/todos-ngrx',
'app/todos-firebase'
/** @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',
'lodash': 'vendor/lodash',
'main': 'main.js'
},
packages: cliSystemConfigPackages
});

// Apply the user's configuration.
System.config({ map, packages });

打字.json

{
"ambientDevDependencies": {
"angular-protractor": "registry:dt/angular-protractor#1.5.0+20160425143459",
"jasmine": "registry:dt/jasmine#2.2.0+20160412134438",
"selenium-webdriver": "registry:dt/selenium-webdriver#2.44.0+20160317120654"
},
"ambientDependencies": {
"es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654"
},
"dependencies": {
"lodash": "registry:npm/lodash#4.0.0+20160723033700"
}
}

这是我尝试在其中使用 lodash 的存储库和文件。您可以克隆存储库和 npm install,npm start。然后在以下文件中取消注释 lodash 导入以查看错误发生。

https://github.com/ryanlangton/angular2-rxjs/blob/master/src/app/http-finish/http-finish.component.ts

最佳答案

请运行

typings install lodash=dt~lodash -G

这将为 lodash 安装类型并在相关文件上更新它。如果它不会那么请手动更新 typings.d.ts 文件,在 typings 目录中使用 lodash 定义的全局安装位置。

Eg: /// <reference path="../typings/global/lodash/index.d.ts" />

为了 typescript 解决纯 JS 库的外部依赖,我们必须提供类型定义。以上步骤同上。

您可能需要调整 system.config.ts 文件,因为 lodash 是纯 JS 库,在 lodash 目录中不会有 index.js

map: {
lodash: 'vendor/lodash/lodash.js
}

并从包中删除此代码

'lodash': {
main:'index.js',
defaultExtension:'js'
}

或者让您按原样进行映射并调整 packages 使其像

'lodash': {
main:'lodash.js',
defaultExtension:'js'
}

详情see

关于angularjs - 使用 lodash 构建 Angular 2 CLI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38544282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com