我现在真的很困惑,因为我得了ERROR TypeError: "_this.device.addKeysToObj is not a function"分.但是我实现了这个函数,所以我不知道是什么问题,也不知道为什么它不可调用.我曾在Firefox和chrome上try 过这段代码,但都出现了相同的错误.

错误在第this.device.addKeysToObj(this.result.results[0]);

这是我的课:

export class Device {
    id: number;
    deviceID: string;
    name: string;
    location: string;
    deviceType: string;
    subType: string;
    valueNamingMap: Object;

    addKeysToObj(deviceValues: object): void {
        for (let key of Object.keys(deviceValues).map((key) => { return key })) {
            if (!this.valueNamingMap.hasOwnProperty(key)) {
                this.valueNamingMap[key] = '';
            }
        }
        console.log(this, deviceValues);
    }
}

这就是号召:

export class BatterieSensorComponent implements OnInit {
    @Input() device: Device;
    public result: Page<Value> = new Page<Value>();

    //[..]

    ngOnInit() {
      this.valueService.list('', this.device).subscribe(
        res => {
          console.log(this.device);  // NEW edit 1
          this.result = res;
          if (this.result.count > 0) 
          {
            this.device.addKeysToObj(this.result.results[0]);
          }
        }
      )
    }
}

Edit 1

日志(log)this.device请参见上面代码中的注释:

{
    deviceID: "000000001" 
    deviceType: "sensor"    ​
    id: 5    ​
    location: "-"
​    name: "Batteries"    ​
    subType: "sensor"    ​
    valueNamingMap:
      Object { v0: "vehicle battery", v1: "Living area battery" }
    <prototype>: Object { … } 
}

Edit 2

设备的一部分.服务代码:

list(url?: string, deviceType?: string, subType?: string): Observable<Page<Device>> {
  if(!url) url = `${this.url}/devices/`;
  if(deviceType) url+= '?deviceType=' + deviceType;
  if(subType) url+= '&subType=' + subType;

  return this.httpClient.get<Page<Device>>(url, { headers: this.headers })
    .pipe(
      catchError(this.handleError('LIST devices', new Page<Device>()))
    );
}

父组件中的调用:

ngOnInit() {
  this.deviceService.list('', 'sensor', ).subscribe(
    res => { 
      this.devices = res.results;
    }
  )
}

模板:

<div class="mdl-grid">
  <div class="mdl-cell mdl-cell--6-col mdl-cell--6-col-tablet" *ngFor="let device of devices">
    <app-batterie-sensor [device]="device"></app-batterie-sensor>
  </div>
</div>

推荐答案

Original answer

这是Typescript的常见问题,你说deviceDevice类型,但它不是.它的所有属性都与Device相同,但因为它不是Device,所以没有预期的方法.

您需要确保为Page中的每个条目实例化Device,可能是在父组件的ngOnInit中:

我不知道Page的 struct ,但如果它是一个数组,请try 以下方法.

ngOnInit() {
  this.deviceService.list('', 'sensor', ).subscribe(
    res => { 
      this.devices = res.results.map(x => Object.assign(new Device(), x));
    }
  )
}

Further explanation

让我们try 一个typescript示例,因为这种行为与Angular无关.我们将使用localStorage来表示来自外部源的数据,但这与HTTP的工作原理相同.

interface SimpleValue {
    a: number;
    b: string;
}

function loadFromStorage<T>(): T {
    // Get from local storage.
    // Ignore the potential null value because we know this key will exist.
    const storedValue = localStorage.getItem('MyKey') as string;

    // Note how there is no validation in this function.
    // I can't validate that the loaded value is actually T
    // because I don't know what T is.
    return JSON.parse(storedValue);
}

const valueToSave: SimpleValue = { a: 1, b: 'b' };
localStorage.setItem('MyKey', JSON.stringify(valueToSave));

const loadedValue = loadFromStorage<SimpleValue>();

// It works!
console.log(loadedValue);

很好,太棒了.typescript接口纯粹是一种编译时 struct ,与类不同,它在JavaScript中没有类似功能——它只是一个开发人员提示.但这也意味着,如果你为一个外部值创建一个接口,比如上面的SimpleValue,然后得到wrong,那么编译器仍然会相信你知道你在说什么,它不可能在编译时验证这个.

从外部源加载类怎么样?这有什么不同?如果我们以上面的例子,在不改变任何其他内容的情况下,将SimpleValue改为一个类,那么它仍然有效.但这是有区别的.与接口不同,类被转换成JavaScript类似功能,换句话说,它们存在于编译点之后.在我们上面的示例中,这不会导致问题,所以让我们try 一个确实会导致问题的示例.

class SimpleClass {
    constructor(public a: number, public b: string) { }

    printA() {
        console.log(this.a);
    }
}

const valueToSave: SimpleClass = new SimpleClass(1, 'b');
localStorage.setItem('MyKey', JSON.stringify(valueToSave));

const loadedValue = loadFromStorage<SimpleClass>();

console.log(loadedValue.a); // 1
console.log(loadedValue.b); // 'b'
loadedValue.printA(); // TypeError: loadedValue.printA is not a function

加载的值具有我们预期的属性,但没有方法,哦!问题是调用new SimpleClass时会创建方法.当我们创建valueToSave时,我们确实实例化了这个类,但后来我们将它转换成一个JSON字符串并将其发送到其他地方,JSON没有方法的概念,因此信息丢失了.当我们在loadFromStorage中加载数据时,not调用了new SimpleClass,我们只是相信调用方知道存储的类型.

我们该如何应对?让我们回到Angular ,考虑一个常见的用例:日期.JSON没有日期类型,JavaScript有,那么我们如何从服务器检索日期并将其作为日期使用呢?这是我喜欢使用的一种模式.

interface UserContract {
    id: string;
    name: string;
    lastLogin: string; // ISO string representation of a Date.
}

class UserModel {
    id: string; // Same as above
    name: string; // Same as above
    lastLogin: Date; // Different!

    constructor(contract: UserContract) {
        // This is the explicit version of the constructor.
        this.id = contract.id;
        this.name = contract.name;
        this.lastLogin = new Date(contract.lastLogin);

        // If you want to avoid the boilerplate (and safety) of the explicit constructor
        // an alternative is to use Object.assign:
        // Object.assign(this, contract, { lastLogin: new Date(contract.lastLogin) });
    }

    printFriendlyLastLogin() {
        console.log(this.lastLogin.toLocaleString());
    }
}

import { HttpClient } from '@angular/common/http';
import { Injectable, Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable({
    providedIn: 'root'
})
class MyService {
    constructor(private httpClient: HttpClient) { }

    getUser(): Observable<UserModel> {
        // Contract represents the data being returned from the external data source.
        return this.httpClient.get<UserContract>('my.totally.not.real.api.com')
            .pipe(
              map(contract => new UserModel(contract))
            );
    }
}

@Component({
    // bla bla
})
class MyComponent implements OnInit {
    constructor(private myService: MyService) { }

    ngOnInit() {
        this.myService.getUser().subscribe(x => {
            x.printFriendlyLastLogin(); // this works
            console.log(x.lastLogin.getFullYear()); // this works too
        });
    }
}

也许有点冗长,但这是我用来处理来自扁平后端合同的丰富前端模型的最健壮、最灵活的模式.

Typescript相关问答推荐

TypScript中的算法运算式

对于使用另一个对象键和值类型的对象使用TS Generics

带有联合参数的静态大小数组

替代语法/逻辑以避免TS变量被分配之前使用." "

如何判断输入是否是TypeScript中的品牌类型?

如何将类型从变量参数转换为返回中的不同形状?

具有继承的基于类的react 组件:呈现不能分配给基类型中的相同属性

如何在单击停止录制按钮后停用摄像机?(使用React.js和Reaction-Media-Recorder)

TypeScrip表示该类型不可赋值

try 使Angular依赖注入工作

基于未定义属性的条件属性

可赋值给类型的类型,从不赋值

如何创建一个将嵌套类属性的点表示法生成为字符串文字的类型?

如何使用IsEqual创建断言类型相等的实用程序

T的typeof键的Typescript定义

为什么类方法参数可以比接口参数窄

如何根据Typescript 中带有泛型的对象嵌套键数组获取数组或对象的正确类型?

如何使用 runInInjectionContext 中的参数测试功能性路由防护

使用 fp-ts 时如何使用 TypeScript 序列化任务执行?

可选通用映射器函数出错