在 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
给了我错误 ';'预期.
.当我尝试使用第二种方法时,出现错误:
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"'、'import d from "mod"' 或其他模块格式.)
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 项目 https://github.com/awslabs/aws-cognito-angular2-quickstart/blob/master/src/app/service/aws.service.ts 但他们的项目没有解释他们是如何导入的开发工具包.
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"]
:
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屋!