gpt4 book ai didi

angular2 无法解析 'RequestOptions' 的所有参数(?)

转载 作者:太空狗 更新时间:2023-10-29 17:50:52 25 4
gpt4 key购买 nike

我试图将 http 提供程序导入我自己的提供程序之一。但它抛出我上面提到的错误。

首先,我将 Http 导入到我的 ActionTracker-Service 中。但是后来我得到了错误

Exception No Provider for AppComponent -> ActionTracker -> Http

所以我也将 Http 导入到我的 boot.ts 中。

index.html

<html>
<head>
<title>Angular 2 QuickStart</title>

<!-- 1. Load libraries -->
<script src="angular2/bundles/angular2-polyfills.js"></script>
<script src="systemjs/dist/system.src.js"></script>

<script src="rxjs/bundles/Rx.js"></script>
<script src="angular2/bundles/angular2.dev.js"></script>
<script src="angular2/bundles/http.dev.js"></script>
<!-- 2. Configure SystemJS -->
<script>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('app/boot')
.then(null, console.error.bind(console));
</script>

<link href="bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet">
<link href="assets/styles/main.css" rel="stylesheet">
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>

启动.ts

import {bootstrap}    from 'angular2/platform/browser'
import {AppComponent} from './app.component'
import {Http, ConnectionBackend, RequestOptions} from 'angular2/http'

bootstrap(AppComponent, [ConnectionBackend, RequestOptions, Http]);

Action 追踪器.service.ts

import {Injectable} from 'angular2/core';
import {Track} from './track'
import {Http} from 'angular2/http'

@Injectable()
export class ActionTracker {
constructor(private _http:Http){}
}

即将出现的错误是:

Cannot resolve all parameters for 'RequestOptions'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'RequestOptions' is decorated with Injectable.
Error: Cannot read property 'getOptional' of undefined(…)

当我仅在 boots.ts 文件中传递“Http”时,我收到了缺少 ConnectionBackend 和 RequestOptions 的错误。所以我添加了两者,但现在我陷入了 RequestOptions 的错误。

谁能告诉我为什么会出现这个错误?为什么我还需要将导入的提供程序作为数组传递到引导函数中?

谢谢! :)

最佳答案

RequestOptions 的构造函数接受类型为 RequestOptionsArgs 的参数。

为防止出现此类问题,您应该在bootstrap 函数中使用HTTP_PROVIDERS 数组:

import {bootstrap} from 'angular2/platform/browser';
import {HTTP_PROVIDERS} from 'angular2/http';

bootstrap(AppComponent, [
HTTP_PROVIDERS
]);

Angular2 提供了这样的数组来简单地配置所有提供者的特定功能,如 HTTP、路由、...

我猜您在组件的 providers 属性中指定了 ActionTracker。这可能没问题,除非您想为 Angular2 应用程序的所有元素共享服务实例。在这种情况下,您还需要在 bootstrap 函数中指定它。

如果你想了解更多关于 Angular2 中的依赖注入(inject),这个答案可以提供一些提示:

希望对你有帮助,蒂埃里

关于angular2 无法解析 'RequestOptions' 的所有参数(?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35055424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com