阅读背景:

在Angular 2应用程序中导入AWS SDK

来源:互联网 

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:

我试图在我的Angular 2应用程序中使用AWS SDK,但是我很困难。以下是我采取的步骤:

  1. Installed the aws sdk into my Angular 2 application using npm install aws-sdk
  2. 使用npm install aws-sdk将aws sdk安装到我的Angular 2应用程序中
  3. Installed the types using npm install --save-dev @types/node
  4. 使用npm install --save-dev @ types / node安装类型
  5. Attempted to include the AWS modules several different ways in my Angular 2 service: declare var AWS: any;, import AWS = require('aws-sdk');, and finally import * as AWS from 'aws-sdk';.
  6. 试图在我的Angular 2服务中包含几种不同的AWS模块:声明var AWS:any;,导入AWS = require('aws-sdk');,最后从'aws-sdk'中导入*作为AWS。

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:

当我尝试使用第一种和第三种类型的导入时,在尝试访问AWS对象中的库之前,我没有得到转换器错误,即AWS.config.region ='us-west-2给我错误';'当我尝试使用第二种方法时,我得到错误:

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.)

定位ECMAScript 2015模块时无法使用导入分配。考虑使用'import * as ns from“mod”','import {a} from“mod”','import d from“mod”'或其他模块格式。)

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.

我错过了一步吗?我看过github项目https://github.com/awslabs/aws-cognito-angular2-quickstart/blob/master/src/app/service/aws.service.ts但是他们的项目没有解释他们是怎么回事导入了SDK。

2 个解决方案

#1


6  

You need to:

你需要:

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

Now in your src folder, you will find tsconfig.app.ts (make sure not to get confused with tsconfig.ts at the root).

现在在你的src文件夹中,你会发现tsconfig.app.ts(确保不要与root用户的tsconfig.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"]:

types行是空的,在编辑之前看起来像这样:“types”:[]。你需要编辑它。添加“types”后,tsconfig.app.ts应该如下所示:[“node”]:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
  "outDir": "../out-tsc/app",
  "module": "es2015",
  "baseUrl": "",
  "types": ["node"]
},
"exclude": [
  "test.ts",
  "**/*.spec.ts"
  ]
}

Now you can just

现在你可以

import * as AWS from 'aws-sdk';

#2


2  

I am currently working with AWS with angular as my frontend, and am using the same angular cognito quickstart project for my authentication and authorisation flow.

我目前正在使用AWS作为我的前端,并使用相同的角度认知快速启动项目进行身份验证和授权流程。

For installation, I used npm install aws-sdk --save

为了安装,我使用了npm install aws-sdk --save

The aws-sdk is under dependencies' in my package.json:

我的package.json中的aws-sdk属于依赖项:

"dependencies": {
    .......
    "aws-sdk": "^2.41.0",
    .......
}

If you are using angular cli, make sure you include the aws-sdk into your angular-cli.json

如果您使用的是角度cli,请确保将aws-sdk包含在angular-cli.json中

After these I used declare var AWS: any; And I am able to use the library without any problem.

在这之后我使用了declare var AWS:any;我可以毫无问题地使用该库。


分享到: