Importing AWS SDK in Angular 2 Application

2019-02-14 07:42发布

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:

  1. Installed the aws sdk into my Angular 2 application using npm install aws-sdk
  2. Installed the types using npm install --save-dev @types/node
  3. 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';.

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:

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.

2条回答
smile是对你的礼貌
2楼-- · 2019-02-14 08:23

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.

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

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

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

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

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

查看更多
forever°为你锁心
3楼-- · 2019-02-14 08:36

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

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"
  ]
}

Now you can just

import * as AWS from 'aws-sdk';
查看更多
登录 后发表回答