在Angular 2应用程序中导入AWS开发工具包 [英] Importing AWS SDK in Angular 2 Application

查看:96
本文介绍了在Angular 2应用程序中导入AWS开发工具包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在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:

  1. 使用npm install aws-sdk
  2. 将aws sdk安装到我的Angular 2应用程序中
  3. 使用npm install --save-dev @types/node
  4. 安装类型
  5. 试图在我的Angular 2服务中以几种不同的方式包含AWS模块:declare var AWS: any;import AWS = require('aws-sdk');,最后是import * as AWS from 'aws-sdk';.
  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';.

当我尝试使用第一种和第三种类型的导入时,在尝试访问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.)

我错过了一步吗?我看过github项目

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆