在Angular 2应用程序中导入AWS开发工具包 [英] Importing AWS SDK in Angular 2 Application
问题描述
我试图在Angular 2应用程序中使用AWS开发工具包,并且陷入了困境.这是我已采取的步骤:
I am attempting to use the AWS SDK in my Angular 2 application and am getting quite stuck. Here are the steps I have taken:
- 使用
npm install aws-sdk
将aws sdk安装到我的Angular 2应用程序中
- 使用
npm install --save-dev @types/node
安装类型
- 试图在我的Angular 2服务中以几种不同的方式包含AWS模块:
declare var AWS: any;
,import AWS = require('aws-sdk');
,最后是import * as AWS from 'aws-sdk';
.
- Installed the aws sdk into my Angular 2 application using
npm install aws-sdk
- Installed the types using
npm install --save-dev @types/node
- Attempted to include the AWS modules several different ways in my Angular 2 service:
declare var AWS: any;
,import AWS = require('aws-sdk');
, and finallyimport * as AWS from 'aws-sdk';
.
当我尝试使用第一种和第三种类型的导入时,在尝试访问AWS对象中的库之前,我没有收到编译器错误,即AWS.config.region = 'us-west-2
给我错误';' expected.
.当我尝试使用第二种方法时,出现错误:
when I attempt to use the first and third type of import, I don't get a transpiler error until I attempt to access a library within the AWS object, i.e. AWS.config.region = 'us-west-2
gives me the error ';' expected.
. And when I attempt to use the second method, I get the error:
定位ECMAScript 2015时无法使用导入分配 模块.考虑使用'import * as ns from"mod"','import {a} from "mod"','从"mod"'导入d或其他模块格式.)
Import assignment cannot be used when targeting ECMAScript 2015 modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.)
Am I missing a step? I've looked at the github project https://github.com/awslabs/aws-cognito-angular2-quickstart/blob/master/src/app/service/aws.service.ts but their project doesn't explain how they imported the SDK.
推荐答案
您需要:
npm install aws-sdk -S
然后确保已安装此类型(如果不确定,请运行下面的代码):
Then make sure you have this type installed (if you are not sure, then just run the code below):
npm install --save-dev @types/node
现在在src文件夹中,您会找到tsconfig.app.ts
(请确保不要在根目录上与tsconfig.ts
混淆).
Now in your src folder, you will find tsconfig.app.ts
(make sure not to get confused with tsconfig.ts
at the root).
types行为空,并且在编辑之前如下所示:"types": []
.您需要对其进行编辑.
添加"types": ["node"]
后,tsconfig.app.ts应该看起来像这样:
types line is empty and looks like this prior to editing: "types": []
. You need to edit it.
The tsconfig.app.ts should look like this after you add "types": ["node"]
:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"module": "es2015",
"baseUrl": "",
"types": ["node"]
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
现在您可以
import * as AWS from 'aws-sdk';
这篇关于在Angular 2应用程序中导入AWS开发工具包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!