2017-06-24 11 views
1

で外部モジュールにアクセスすることができ、私はインターフェイスにアクセスしたい以下はどのように私はtypescriptです

ROOT /データベース/ models.ts

module Model { 
    export interface Inbox { 
    title: string; 
    msg: string; 
    } 
    export interface User{ 
    Id: string; 
    Name: string; 
    } 
} 

のように私は、モジュールを含むファイルを持っていますメインファイルから。私は

ROOT /ページ/ main.ts

import { Model} from '../database/models'; //<-- here is my problem 

export class WorkerPostbidPage { 

    myinbox:Model.Inbox; // <-- And I want to call it as this 

    TestMethod() { 
    this.myinbox = { 
     title:"new msg", 
     msg:"you have an alert" 
    } 
    } 
} 

しかし、試したものを見ることが正常に動作し、以下のように私はmain.tsに外部のモデルファイルの内容をコピーするとき

module Model { 
    export interface Inbox { 
    title: string; 
    msg: string; 
    } 
    export interface User{ 
    Id: string; 
    Name: string; 
    } 
} 

export class WorkerPostbidPage { 

    myinbox:Model.Inbox; // <-- And I want to call it as this 

    TestMethod() { 
    this.myinbox = { 
     title:"new msg", 
     msg:"you have an alert" 
    } 
    } 
} 

私は何か間違っているかもしれないと思うか、いくつかの部分が欠けていると思います。

+0

を動作するはずと仮定すると、私はそれを試してみました、と私はモデルの名前空間を介してそれらにアクセスすることはできませんなぜそのほか正しく –

答えて

0

それがES6モジュールに準拠するようにmodel.tsファイルを書きます。

export interface Inbox { 
    title: string; 
    msg: string; 
    } 
    export interface User{ 
    Id: string; 
    Name: string; 
    } 

次に、このようにインポートする必要があります。

import { Inbox } from '../database/models'; 
+0

をファイルを見つけることはありません。それはいくつかの基準に反しますか?私は本当に名前空間でそれを好みました。 –

+0

しかし、同じファイルにコンテンツをコピーする私の2番目のアプローチを見てください。それは完璧に働いた。 –

+0

ええ、モジュール自体が書かれた方法はエクスポートされませんでした。したがって、輸入することはできませんでした。 – toskv

0

この方法を使用して外部モジュールをインポートすることはできません。 外部モジュールは最終的にグローバルスコープに追加されるため、そのまま使用してください。

あなたはAMDのモジュールを使用している、このようなものが

declare module Model{ 
    interface Inbox{ 
     title: string, 
     msg: string 
    } 
} 

/// <amd-dependency path="../database/models"/> 

export class WorkerPostbidPage { 

    myinbox:Model.Inbox; // <-- And I want to call it as this 

    TestMethod() { 
     this.myinbox = { 
      title:"new msg", 
      msg:"you have an alert" 
     } 
    } 
} 
+0

amdとは何ですか? –

+0

非同期モジュール定義http://requirejs.org/docs/whyamd.html –