2016-04-26 12 views
0

プラグインのコードの論理的な流れに本当に混乱しました。私は私が持っている、このseedを使用していますが、ちょうどにconsole.logが含まれているプラ​​グインから関数を呼び出すデモアプリケーション、中にボタンを作成しようとしています:私のXMLでNativescriptのボタンタップからプラグイン関数を参照する

//yourPlugin.common.ts: 
import * as app from 'application'; 
import * as dialogs from 'ui/dialogs'; 

export class Common { 
    public message: string; 

    constructor() { 
    this.message = Utils.SUCCESS_MSG(); 
    } 
} 

export class Utils { 
    //Utils Stuff - not relevant for this issue 
} 

export function Click() { 
     console.log("Clicked"); 
    } 


//demo/app/main-view-model.ts: 

import {Observable} from 'data/observable'; 
import {YourPlugin} from 'nativescript-yourplugin'; 

export class HelloWorldModel extends Observable { 
    public message: string; 
    private yourPlugin: YourPlugin; 
    private Click: YourPlugin.Click; 

    constructor() { 
    super(); 

    this.yourPlugin = new YourPlugin(); 
    this.message = this.yourPlugin.message; 
    } 
} 

、その後に、私が追加しましたアプリのページへ<Button text="This is Button!" tap="Click" />しかし、私がボタンを押すと、コンソールログが起動していない、何が間違っていますか?

更新:

のxml:

<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded"> 
    <StackLayout> 
    <Label text="{{ message }}" class="message" textWrap="true"/> 
    <Button text="This is Button!" tap="{{ Click }}" /> 
    </StackLayout> 
</Page> 

メインpage.ts:

import * as observable from 'data/observable'; 
import * as pages from 'ui/page'; 
import {HelloWorldModel} from './main-view-model'; 

// Event handler for Page "loaded" event attached in main-page.xml 
export function pageLoaded(args: observable.EventData) { 
    // Get the event sender 
    var page = <pages.Page>args.object; 
    page.bindingContext = new HelloWorldModel(); 

} 

答えて

2

この構文:から

import {YourPlugin} from 'nativescript-yourplugin' 

のみをインポートという名前のクラスYourPluginnativescript-yourpluginモジュール。したがって、あなたはmain-view-model.tsファイル内の他のものにアクセスすることはできません。プラグインで定義されている他のものにアクセスするには、モジュール全体をインポートする必要があります。また、HelloWorldModelにはClickが関数でなければならず、あなたはそれをある種の型として定義するだけです。 Click以上の機能は公開されていません。

<Button text="This is Button!" tap="{{ Click }}" /> 
+0

私はあなたの正確なコードをコピーした場合、+輸入:あなたはこのようになっているはずですHelloWorldModelあなたのボタンのインスタンスにあなたのXMLを結合していると仮定すると、最終的

import {Observable} from 'data/observable'; import nsPlugin = require('nativescript-yourplugin'); export class HelloWorldModel extends Observable { public message: string; private yourPlugin: nsPlugin.YourPlugin; public Click = nsPlugin.Click; constructor() { super(); this.yourPlugin = new nsPlugin.YourPlugin(); this.message = this.yourPlugin.message; } } 

そして:だからあなたのコードは次のようになりますこのプラグインは 'natives-nsplugin'のnsPluginとしてインポートしています; 'まだボタンプレスでconsole.logを取得できません... –

関連する問題