2016-11-01 114 views
0

我已經定義像這樣的模塊中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並不突出什麼...似乎是確定。

回答

0

錯誤的最可能原因是您的定義文件未包含在編譯上下文中,因此您的declare module 'target'永遠不會被調用。

有幾種方法可以選擇包括它:

  1. 添加///<reference path="path/to/your/target.d.ts" />您switch.component.ts的頂部。

  2. 添加path/to/your/target.d.ts"file":[]陣列在tsconfig.json

  3. 添加path/to/your/target.d.ts(或將匹配它的等效文件模式)的"include":[]陣列在tsconfig.json

一旦你包括在你的編譯背景下target.d.ts,那麼你應該能夠import {Target, Device, Property} from 'target'在應用程序中的任何地方。