gpt4 book ai didi

javascript - 在 Angular2 rc5 项目中导入 Angular2-Cookie

转载 作者:行者123 更新时间:2023-12-03 06:12:16 27 4
gpt4 key购买 nike

我正在尝试在我刚刚启动的 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)'
]
});
};

运行构建

在我的 dist/vendor 文件夹中创建 angular2-cookie 文件夹

我的 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"
}
}

最佳答案

您需要在这些对象中设置 map 和包。不要修改这些行以下的任何内容。

/** 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,但新版本即将推出。我想它会在今天或明天发布。

关于javascript - 在 Angular2 rc5 项目中导入 Angular2-Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39268655/

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