我正在尝试在刚启动的angular2 rc5项目中导入angular2 cookie模块。
我用npm
安装了模块npm install angular2-cookie
编辑了我的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)',
'@angular/**/*.+(js|js.map)',
'materialize-css/bin/materialize.css',
'materialize-css/bin/materialize.js',
'socket.io-client/socket.io.js',
'angular2-cookie/**/*.+(ts|js|js.map)'
]
});
};
运行构建
angular2-cookie文件夹在我的dist / vendor文件夹中创建
我的system-config.ts看起来像这样
"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 = {
};
/** User packages configuration. */
const packages: any = {
};
////////////////////////////////////////////////////////////////////////////////////////////////
/***********************************************************************************************
* 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',
"socket.io-client": "node_modules/socket.io-client/socket.io.js",
"angular2-cookie": 'vendor/angular2-cookie'
},
packages: cliSystemConfigPackages,
"socket.io-client": {"defaultExtension": "js"},
'angular2-cookie' : { defaultExtension: 'js',main: 'core.js'}
});
// Apply the user's configuration.
System.config({ map, packages });
我设法导入它并在我的组件文件中添加一些代码
import {CookieService} from 'angular2-cookie/core';
但由于某些原因,当我尝试为应用程序提供服务时,无法加载以下错误
zone.js:101 GET http://localhost:4200/vendor/angular2-cookie/core 404 (Not Found)scheduleTask @ zone.js:101ZoneDelegate.scheduleTask @ zone.js:336Zone.scheduleMacroTask @ zone.js:273(anonymous function) @ zone.js:122send @ VM308:3fetchTextFromURL @ system.src.js:1154(anonymous function) @ system.src.js:1735ZoneAwarePromise @ zone.js:584(anonymous function) @ system.src.js:1734(anonymous function) @ system.src.js:2759(anonymous function) @ system.src.js:3333(anonymous function) @ system.src.js:3600(anonymous function) @ system.src.js:3985(anonymous function) @ system.src.js:4448(anonymous function) @ system.src.js:4700(anonymous function) @ system.src.js:406ZoneDelegate.invoke @ zone.js:323Zone.run @ zone.js:216(anonymous function) @ zone.js:571ZoneDelegate.invokeTask @ zone.js:356Zone.runTask @ zone.js:256drainMicroTaskQueue @ zone.js:474ZoneTask.invoke @ zone.js:426
2016-09-01 10:48:02.373 zone.js:461 Unhandled Promise rejection: Error: XHR error (404 Not Found) loading http://localhost:4200/vendor/angular2-cookie/core
at XMLHttpRequest.wrapFn [as _onreadystatechange] (http://localhost:4200/vendor/zone.js/dist/zone.js:769:30)
at ZoneDelegate.invokeTask (http://localhost:4200/vendor/zone.js/dist/zone.js:356:38)
at Zone.runTask (http://localhost:4200/vendor/zone.js/dist/zone.js:256:48)
at XMLHttpRequest.ZoneTask.invoke (http://localhost:4200/vendor/zone.js/dist/zone.js:423:34)
Error loading http://localhost:4200/vendor/angular2-cookie/core as "angular2-cookie/core" from http://localhost:4200/app/app.component.js ; Zone: <root> ; Task: Promise.then ; Value: Error: Error: XHR error (404 Not Found) loading http://localhost:4200/vendor/angular2-cookie/core(…)consoleError @ zone.js:461_loop_1 @ zone.js:490drainMicroTaskQueue @ zone.js:494ZoneTask.invoke @ zone.js:426
2016-09-01 10:48:02.378 zone.js:463 Error: Uncaught (in promise): Error: Error: XHR error (404 Not Found) loading http://localhost:4200/vendor/angular2-cookie/core(…)
看起来像angular2 cookie文件夹无法访问(或未正确提供),但无法找到原因。
我的package.json依赖项:
"dependencies": {
"@angular/common": "2.0.0-rc.5",
"@angular/compiler": "2.0.0-rc.5",
"@angular/core": "2.0.0-rc.5",
"@angular/forms": "0.3.0",
"@angular/http": "2.0.0-rc.5",
"@angular/platform-browser": "2.0.0-rc.5",
"@angular/platform-browser-dynamic": "2.0.0-rc.5",
"@angular/router": "3.0.0-rc.1",
"angular2-cookie": "^1.2.2",
"async": "^2.0.1",
"clone": "^1.0.2",
"es6-shim": "0.35.1",
"express": "^4.14.0",
"materialize-css": "^0.97.7",
"moment": "^2.14.1",
"redis": "^2.6.2",
"reflect-metadata": "0.1.3",
"rethinkdb": "^2.3.2",
"rxjs": "^5.0.0-beta.6",
"socket.io": "^1.4.8",
"socket.io-client": "^1.4.8",
"systemjs": "0.19.37",
"typings": "^1.3.1",
"zone.js": "0.6.17"
},
"devDependencies": {
"angular-cli": "1.0.0-beta.10",
"codelyzer": "0.0.20",
"ember-cli-inject-live-reload": "1.4.0",
"jasmine-core": "2.4.1",
"jasmine-spec-reporter": "2.5.0",
"karma": "0.13.22",
"karma-chrome-launcher": "0.2.3",
"karma-jasmine": "0.3.8",
"protractor": "3.3.0",
"ts-node": "0.5.5",
"tslint": "3.11.0",
"typescript": "1.8.10",
"typings": "1.3.1"
}
}
答案 0 :(得分:1)
您需要在这些对象中设置地图和包。不要修改这些行以下的任何内容。
/** Map relative paths to URLs. */
const map: any = {
'angular2-cookie': 'vendor/angular2-cookie'
};
/** User packages configuration. */
const packages: any = {
'angular2-cookie': {main: 'core.js', defaultExtension: 'js'},
};
请注意,目前angular2-cookie
不适用于rc6,但新版本正在进行中。我想它会在今天或明天发布。
答案 1 :(得分:0)
您在配置文件"angular2-cookie": 'vendor/angular2-cookie'
中进行了映射,
所以你需要拨打import {CookieService} from 'angular2-cookie';
应该可以使用,否则请检查此文件夹/vendor/angular2-cookie/
,并检查是否存在core.js
文件,您在System.config 'angular2-cookie' : { defaultExtension: 'js',main: 'core.js'}
中为此模块设置了默认文件。