加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

typescript – Angular2 http缺少.map函数

发布时间:2020-12-17 09:16:48 所属栏目:安全 来源:网络整理
导读:参见英文答案 Angular 2 HTTP GET with TypeScript error http.get(…).map is not a function in [null]4个 我在 tutorial中发现了这个示例代码: getRandomQuote() { this.http.get('http://localhost:3001/api/random-quote') .map(res = res.text()) .su
参见英文答案 > Angular 2 HTTP GET with TypeScript error http.get(…).map is not a function in [null]4个
我在 tutorial中发现了这个示例代码:
getRandomQuote() {
  this.http.get('http://localhost:3001/api/random-quote')
    .map(res => res.text())
    .subscribe(
      data => this.randomQuote = data,err => this.logError(err),() => console.log('Random Quote Complete')
    );
}

但是当尝试使用它时,我只会得到TypeError:this.http.get(…).map不是[null]中的函数:

getChannels():Promise<Channel> {
    return this.http.get('...')
        .map(function (response:Response) {
            ...
        }).toPromise();
}

我的Typescript编译器告诉我,这些方法是可用的,但是当检查http.get()的返回值时,它们是丢失的.

我使用当前angualar2启动指南的package.json:

"dependencies": {
  "angular2": "2.0.0-beta.0","systemjs": "0.19.6","es6-promise": "^3.0.2","es6-shim": "^0.33.3","reflect-metadata": "0.1.2","rxjs": "5.0.0-beta.0","zone.js": "0.5.10"
},

<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/http.dev.js"></script>

在这一点上我有什么想法可能会出错?

您将希望您的index.html看起来像这样,所以system.js可以找到您在组件中导入的所有rxjs依赖项.
<script src="/lib/anguar2/angular2-polyfills.js"></script>
        <script src="/lib/es6-shim/es6-shim.js"></script>
        <script src="/lib/systemjs/system.src.js"></script>

        <script>
            System.config({
                defaultJSExtensions: true,packages: {
                    app: {
                        format: 'register'
                    }
                },map: {
                    'rxjs':"lib/rxjs"

                }
            });
        </script>
        <script src="/lib/anguar2/angular2.dev.js"></script>
        <script src="/lib/anguar2/router.dev.js"></script>
        <script src="/lib/anguar2/http.js"></script>
        <script>
            System.import('app/boot');
        </script>

确保“lib / rxjs”文件夹具有来自node_modules / rxjs文件夹的所有文件.并不是所有的都将被加载,只有你需要的和它们的依赖关系(system.js会为你量身定制).

现在你可以在你的boot.ts文件中使用它:

import 'rxjs/add/operator/map';

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读