打字稿错误TS2339:类型“ {}”上不存在属性“项目”

克拉维兹

在Angular 2 RC1中打了这个老问题。

导致错误的行是:

  this.project = res.project;

这是我的组件:

            import {Component} from '@angular/core';
            import {ProjectsMainApi} from "../../../services/projects-main";
            import { RouteConfig, RouteParams, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router-deprecated';

            declare var jQuery: any;

            @Component({
                selector: 'projects',
                templateUrl: './app/components/Projects/details/project-single.html',
                directives: [ROUTER_DIRECTIVES]
            })

            export class ProjectDetailsComponent {
                project: Object = {};
                constructor(private _api: ProjectsMainApi, private _params: RouteParams) {
                    this._api.getSinglePortfolio(_params.get("id")).then(
                        (res) => {
                            this.project = res.project;
                        },
                        (error) => {
                            console.error(error);
                        }
                    )
                }
            }

我的服务如下:

            import {Http, Headers, Response} from "@angular/http"
            import {Injectable} from "@angular/core"
            import {IProjectsMain, ISingleProject} from "../interfaces/AvailableInterfaces"
            import 'rxjs/Rx';
            import {Observable} from 'rxjs/Observable';
            import {Observer} from 'rxjs/Observer';
            import 'rxjs/add/operator/share';
            import 'rxjs/add/operator/map';

            @Injectable()
            export class ProjectsMainApi {
                apiUrl: string = "http://www.example.org/api/projects";
                headers: Headers = new Headers;
                project$: Observable<IProjectsMain[]>;
                private _ProjectsMainObserver: Observer<IProjectsMain[]>;
                private _dataStore: {
                    project: IProjectsMain[]
                };

                constructor(private _http: Http) {
                    this.headers.append('Content-Type', 'application/x-www-form-urlencoded');
                    this.headers.append('X-Requested-With', 'XMLHttpRequest');
                    this.project$ = new Observable<IProjectsMain[]>(observer => this._ProjectsMainObserver = observer).share();
                    this._dataStore = { project: [] };
                }

                public getProjectsMain() {
                    this._http.get(this.apiUrl).map(response => response.json()).subscribe(data => {
                        this._dataStore.project = data.project;
                        this._ProjectsMainObserver.next(this._dataStore.project);
                    }, error => console.log('Could not load projects.'),
                        () => "done");
                }

                public getSinglePortfolio(id) {
                    console.log("the id is" + id);
                    return new Promise((resolve, reject) => {
                        this._http.get(this.apiUrl + "/" + id).map((res: Response) => res.json()).subscribe(
                            (res) => {
                                //console.log(res);
                                resolve(res);
                            }, (error) => {
                                reject(error);
                            }
                            );
                    })
                }
            }

并且在此组件中调用的函数是getSinglePortfolio(id)

和杰森

        {"project":[{"id":1,"title":"fdgdfgdfg","slug":"sdfgsdfgsdfg" },   {"id":2,"title":"fdgdfgdfg","slug":"sdfgsdfgsdfg" }]}
yurzui

我认为这是打字稿编译器错误。您可以为此定义自定义界面:

import {Component} from '@angular/core';
import {ProjectsMainApi} from "../../../services/projects-main";
import { RouteConfig, RouteParams, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router-deprecated';

declare var jQuery: any;

interface ProjectResult {
  project: Object
}

@Component({
...

然后为res参数指定类型

 this._api.getSinglePortfolio(_params.get("id")).then(
  (res: ProjectResult ) => {
     this.project = res.project;
  },
  (error) => {
     console.error(error);
  }
)

完整的组件代码

import {Component} from '@angular/core';
import {ProjectsMainApi} from "../../../services/projects-main";
import { RouteConfig, RouteParams, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router-deprecated';

declare var jQuery: any;

interface ProjectResult {
  project: Object
}

@Component({
    selector: 'projects',
    templateUrl: './app/components/Projects/details/project-single.html',
    directives: [ROUTER_DIRECTIVES]
})

export class ProjectDetailsComponent {
    project: Object = {};
    constructor(private _api: ProjectsMainApi, private _params: RouteParams) {
        this._api.getSinglePortfolio(_params.get("id")).then(
            (res: ProjectResult) => {
                this.project = res.project;
            },
            (error) => {
                console.error(error);
            }
        )
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

打字稿| Array.from | 错误TS2339:类型“ ArrayConstructor”上不存在属性“来自”

来自分类Dev

类型“ X []”上不存在属性“ customProperty”。打字稿反应。TS2339

来自分类Dev

打字稿:箭头功能-TS2339:类型“ {}”上不存在属性

来自分类Dev

将api服务更新为打字稿-TS2339:类型“ Http”上不存在属性“ instance”

来自分类Dev

错误 TS:2339 类型 {} 上不存在属性“项目”

来自分类Dev

TS2339:类型“{}”上不存在属性“props”

来自分类Dev

错误:'错误 TS2339:'Response' 类型上不存在属性 'data'。- Angular

来自分类Dev

错误TS2339:类型“响应”上不存在属性“结果”

来自分类Dev

错误TS2339:类型“ Y”上不存在属性“ x”

来自分类Dev

错误TS2339:类型“字符串”上不存在属性“ endsWith”

来自分类Dev

如何修复错误TS2339:类型上不存在属性“ authToken”

来自分类Dev

Express.js / Typescript:错误TS2339:类型“ Response”上不存在属性“ send”

来自分类Dev

错误TS2339:类型“订阅”上不存在属性“订阅”

来自分类Dev

角度:错误TS2339:类型“对象”上不存在属性“数据”

来自分类Dev

角度:错误TS2339:类型“对象”上不存在属性“数据”

来自分类Dev

错误TS2339:类型“ AngularFireAuth”上不存在属性“ auth”

来自分类Dev

TypeScript错误TS2339:类型“ EventTarget”上不存在属性“ matches”

来自分类Dev

错误TS2339:类型'PromiseLike <void>'上不存在属性'catch'

来自分类Dev

错误 TS2339:类型“Angular2”上不存在属性“”

来自分类Dev

错误 TS2339:类型“{}”上不存在属性“includes”

来自分类Dev

错误 TS2339:“FormData”类型上不存在属性“entries”

来自分类Dev

错误 TS2339:“HTMLElement”类型上不存在属性“name”

来自分类Dev

错误 TS2339:“导航器”类型上不存在“相机”属性

来自分类Dev

如何解决错误“TS2339:'JQuery<HTMLElement>' 类型上不存在属性 'gauge'。”

来自分类Dev

错误 TS2339:类型“y[] 上不存在属性“x”

来自分类Dev

带有打字稿的uisng multer:“请求”类型的属性“文件”不存在。ts(2339)

来自分类Dev

angular2:错误:(62,33)TS2339:类型'typeof Injector'上不存在属性'resolveAndCreate'

来自分类Dev

错误 TS2339:“对象”类型上不存在属性“JSON”。".map(resp => resp.JSON())" 角度 4

来自分类Dev

TS2339:类型“元素”上不存在属性“样式”

Related 相关文章

  1. 1

    打字稿| Array.from | 错误TS2339:类型“ ArrayConstructor”上不存在属性“来自”

  2. 2

    类型“ X []”上不存在属性“ customProperty”。打字稿反应。TS2339

  3. 3

    打字稿:箭头功能-TS2339:类型“ {}”上不存在属性

  4. 4

    将api服务更新为打字稿-TS2339:类型“ Http”上不存在属性“ instance”

  5. 5

    错误 TS:2339 类型 {} 上不存在属性“项目”

  6. 6

    TS2339:类型“{}”上不存在属性“props”

  7. 7

    错误:'错误 TS2339:'Response' 类型上不存在属性 'data'。- Angular

  8. 8

    错误TS2339:类型“响应”上不存在属性“结果”

  9. 9

    错误TS2339:类型“ Y”上不存在属性“ x”

  10. 10

    错误TS2339:类型“字符串”上不存在属性“ endsWith”

  11. 11

    如何修复错误TS2339:类型上不存在属性“ authToken”

  12. 12

    Express.js / Typescript:错误TS2339:类型“ Response”上不存在属性“ send”

  13. 13

    错误TS2339:类型“订阅”上不存在属性“订阅”

  14. 14

    角度:错误TS2339:类型“对象”上不存在属性“数据”

  15. 15

    角度:错误TS2339:类型“对象”上不存在属性“数据”

  16. 16

    错误TS2339:类型“ AngularFireAuth”上不存在属性“ auth”

  17. 17

    TypeScript错误TS2339:类型“ EventTarget”上不存在属性“ matches”

  18. 18

    错误TS2339:类型'PromiseLike <void>'上不存在属性'catch'

  19. 19

    错误 TS2339:类型“Angular2”上不存在属性“”

  20. 20

    错误 TS2339:类型“{}”上不存在属性“includes”

  21. 21

    错误 TS2339:“FormData”类型上不存在属性“entries”

  22. 22

    错误 TS2339:“HTMLElement”类型上不存在属性“name”

  23. 23

    错误 TS2339:“导航器”类型上不存在“相机”属性

  24. 24

    如何解决错误“TS2339:'JQuery<HTMLElement>' 类型上不存在属性 'gauge'。”

  25. 25

    错误 TS2339:类型“y[] 上不存在属性“x”

  26. 26

    带有打字稿的uisng multer:“请求”类型的属性“文件”不存在。ts(2339)

  27. 27

    angular2:错误:(62,33)TS2339:类型'typeof Injector'上不存在属性'resolveAndCreate'

  28. 28

    错误 TS2339:“对象”类型上不存在属性“JSON”。".map(resp => resp.JSON())" 角度 4

  29. 29

    TS2339:类型“元素”上不存在属性“样式”

热门标签

归档