找不到Typescript模块

时间:2016-11-01 15:14:44

标签: angular typescript

我在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()变量deviceproperty也是我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没有突出显示任何内容......似乎没问题。

2 个答案:

答案 0 :(得分:0)

导致错误的最可能原因是您的定义文件未包含在编译上下文中,因此永远不会调用getSelectCountSql()

您可以通过多种方式选择加入它:

  1. declare module 'target'添加到switch.component.ts的顶部。

  2. ///<reference path="path/to/your/target.d.ts" />添加到path/to/your/target.d.ts

  3. 中的"file":[]数组中
  4. tsconfig.json(或与之匹配的等效文件模式)添加到path/to/your/target.d.ts中的"include":[]数组。

  5. 在编译上下文中包含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"