我在target.d.ts
declare module "target" {
export interface Group {
name: string;
targets?: Target[];
}
export interface Target {
device: Device;
property: Property;
value?: Value;
}
export interface Value {
value: number;
id?: number;
timestamp?: number;
}
export interface Property {
id: number;
name: string;
channel: number;
type: string;
}
export interface Device {
id: number;
name: string;
type: string;
}
}
现在我在
这样的组件中使用已定义的接口import {Component, OnInit} from '@angular/core';
import {BackendUrl} from '../../../../common/settings';
import {contentHeaders} from "../../../../common/headers";
import {EventService} from "../../../../service/event.service";
import {Input} from "@angular/core/src/metadata/directives";
import {Message} from "stompjs";
import {Target} from "target";
@Component({
selector: 'device-switch',
templateUrl: './switch.component.html'
})
export class SwitchComponent implements OnInit {
@Input()
public title: string;
@Input()
public device: any;
@Input()
public property: any;
private checked: boolean;
constructor(public authHttp: AuthHttp, private eventService: EventService) {
this.checked = false;
}
ngOnInit() {
this.eventService.messages.subscribe(this.on_next);
}
/** Consume a message from the eventService */
public on_next = (message: Message) => {
let data : Target = JSON.parse(message.body);
if(data.device.id === this.device.id && data.property.name === this.property.name) {
this.checked = (data.value.value > 0);
}
};
}
问题是@Input()
变量device
和property
也是我target
模块中定义的接口类型。
但如果我import {Target, Device, Property} from "target";
,编译器会抛出错误
ERROR in ./src/app/component/view/part/device/switch.component.ts
Module not found: Error: Can't resolve 'target' in '~/git/xx/frontend/src/app/component/view/part/device'
@ ./src/app/component/view/part/device/switch.component.ts 16:0-28
@ ./src/app/component/view/part/group.component.ts
@ ./src/app/app.module.ts
@ ./src/main.ts
@ multi main
我不知道这是什么问题...... Intellij没有突出显示任何内容......似乎没问题。
答案 0 :(得分:0)
导致错误的最可能原因是您的定义文件未包含在编译上下文中,因此永远不会调用getSelectCountSql()
。
您可以通过多种方式选择加入它:
将declare module 'target'
添加到switch.component.ts的顶部。
将///<reference path="path/to/your/target.d.ts" />
添加到path/to/your/target.d.ts
"file":[]
数组中
将tsconfig.json
(或与之匹配的等效文件模式)添加到path/to/your/target.d.ts
中的"include":[]
数组。
在编译上下文中包含tsconfig.json
后,您应该可以在应用程序的任何位置target.d.ts
。
答案 1 :(得分:0)
我在Angular 7.2.0项目中遇到了这个问题,将其更新为7.2.8即可解决。
就我而言,我需要强制更新并解决打字稿依赖项:
ng update --all --force
之后
npm install --save typescript@">=3.1.1 <3.3.0"