【问题标题】:Property '' does not exist on type 'Object'“对象”类型上不存在属性“”
【发布时间】:2019-11-28 05:13:42
【问题描述】:

我整天都在工作。一整天一切正常。
重新启动服务器(ng serve)。现在突然出现很多错误。
我设法修复了大部分,但我被这个卡住了。

这是组件 .ts 文件的主要部分:

import { Component, OnInit } from '@angular/core';
import { HttpService } from '../http.service';

@Component({
  selector: 'app-playboard',
  templateUrl: './playboard.component.html',
  styleUrls: ['./playboard.component.scss']
})
export class PlayboardComponent implements OnInit {
  brews: Object;

  constructor(private _http: HttpService) { }

  ngOnInit() {
    this._http.myMethod().subscribe(data => {
      this.brews = data;
      this.dices = this.brews.myBox;
      this.diceSeed = this.brews.boxID;
      console.log(this.brews);
    });
  }

这是 http.service.ts 文件:

import { Injectable } from '@angular/core';
import {HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class HttpService {

  constructor(private http: HttpClient) { }

  myMethod() {
    return this.http.get<Object>('https://localhost:44398/api/boggle');
  }

  myWordMethod(word) {
    var url = 'https://localhost:44398/api/wordpoints/' + word;
    return this.http.get<Object>(url);
  }
}

它工作了一整天,突然出现这些奇怪的错误。
有谁知道可能出了什么问题?非常感谢!

【问题讨论】:

标签: javascript node.js angular typescript npm


【解决方案1】:

从您的 http 调用中删除 Object。使用泛型是可选的,尤其是在您没有输入回复的情况下。

import { Injectable } from '@angular/core';
import {HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class HttpService {

  constructor(private http: HttpClient) { }

  myMethod() {
    return this.http.get('https://localhost:44398/api/boggle');
  }

  myWordMethod(word) {
    var url = 'https://localhost:44398/api/wordpoints/' + word;
    return this.http.get(url);
  }
}

在 brews 上,将其声明为 any:

import { Component, OnInit } from '@angular/core';
import { HttpService } from '../http.service';

@Component({
  selector: 'app-playboard',
  templateUrl: './playboard.component.html',
  styleUrls: ['./playboard.component.scss']
})
export class PlayboardComponent implements OnInit {
  brews: any;

  constructor(private _http: HttpService) { }

  ngOnInit() {
    this._http.myMethod().subscribe(data => {
      this.brews = data;
      this.dices = this.brews.myBox;
      this.diceSeed = this.brews.boxID;
      console.log(this.brews);
    });
  }

【讨论】:

    【解决方案2】:

    您可以通过简单地将它们定义为 any 类型来忽略它们。比如;

    myWordMethod(word: any) {
        ..
    }
    
    this._http.myMethod().subscribe(data: any => {
        ..
    });
    

    也就是说通常首选为 TypeScript 声明实际类型。例如,如果您的 API 发回具有特定属性的通用对象,则将其声明为这样;

    interface MyMethodResponse {
        someProperty: string;
        someNumber: number;
        someArray: string[];
    }
    
    this._http.myMethod().subscribe((myMethodResponse: MyMethodResponse) => {
        // TypeScript now knows that these properties exists on the response object
        console.log(myMethodResponse.someArray);           
        console.log(myMethodResponse.someNumber); 
        console.log(myMethodResponse.someProperty); 
    });
    

    【讨论】:

    • 'data: any' 生成 TS1005,i.imgur.com/G6jsdqe.png
    • @Jeroen 您需要将响应函数括在括号中,以便它的 .subscribe((data: any) => { .. })
    猜你喜欢
    • 2019-10-11
    • 2018-02-10
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-23
    • 2020-09-27
    • 2021-04-02
    相关资源
    最近更新 更多