简体   繁体   English

属性“”在类型“对象”上不存在

[英]Property '' does not exist on type 'Object'

I was working all day in angular. 我整天都在工作。 Everything worked fine all day. 整天一切正常。
Restarted the server (ng serve). 重新启动服务器(ng服务)。 And now suddenly there are alot of errors. 现在突然有很多错误。
I managed to fix most but I am stuck with this one. 我设法解决了大多数问题,但仍坚持使用这一解决方案。

运行服务

This is the main part of the component .ts file: 这是组件.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);
    });
  }

And this is the http.service.ts file: 这是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);
  }
}

It was working all day and suddenly these strange errors appear. 整天都在工作,突然出现了这些奇怪的错误。
Does anyone have a clue of what could be wrong? 有谁知道可能出什么问题了吗? Thanks alot! 非常感谢!

You can ignore these by simply delcaring them as the any type. 您可以通过简单地将它们视为任何类型来忽略它们。 For instance; 例如;

myWordMethod(word: any) {
    ..
}

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

That said declaring the actual type for TypeScript is often preferred. 那就是说通常首选声明TypeScript的实际类型。 For Instance if your API sends back a common object with particular properties then declare it as such; 对于实例,如果您的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); 
});

Remove Object from your http calls. 从您的http调用中删除对象 Using the generic is optional, especially if you haven't typed out your response. 使用泛型是可选的,特别是如果您没有输入响应的话。

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);
  }
}

On brews, declare it as 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);
    });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM