为什么导入后公共财产不可见?

时间:2015-05-31 22:54:32

标签: typescript

我有这个打字稿文件:

/// <reference path="../scripts/typings/requirejs/require.d.ts" />
/// <reference path="../scripts/typings/knockout/knockout.d.ts" />
/// <reference path="../scripts/typings/durandal/durandal.d.ts" />

import app = require("durandal/app");

class AppViewModel {
    currentCourseId = ko.observable();
    currentNativeLanguageId = ko.observable();
    currentLanguageId = ko.observable();
    currentLevelId = ko.observable();
    currentLessonId = ko.observable();

setMessage(message) {
    app.trigger('message:new', message);
   }
}

export = AppViewModel;

目的是将其作为单身人士并将其导入其他地方:

import dataService = require("dataService");
import app = require("durandal/app");
import appViewModel = require("appViewModel");


class SelectNativeLanguage  {
    manager = new breeze.EntityManager('breeze/data');
    items= ko.observableArray();
    section = ko.observable();
    dataService: dataService;
    appViewModel = appViewModel;

edit() {
    app.showDialog('module/nativeLanguage/edit/editNativeLanguage', this);
}

goToItem(item) {
    this.appViewModel.currentNativeLanguageId(item.id()); //Error Here
    app.trigger('home:activateView', 'module/language/selectLanguage');
}
export = SelectNativeLanguage;

这给出了一个编译错误,说'typeof AppViewModel'上不存在currentNativeLanguageId

导入后公共财产不可见,这对我没有意义。

如何解决此问题?

格雷格

1 个答案:

答案 0 :(得分:1)

  

目的是使其成为单身人士

导出实例(单个实例)而不是类。那就是:

import app = require("durandal/app");

export class AppViewModel {
    currentCourseId = ko.observable();
    currentNativeLanguageId = ko.observable();
    currentLanguageId = ko.observable();
    currentLevelId = ko.observable();
    currentLessonId = ko.observable();

    setMessage(message) {
     app.trigger('message:new', message);
    }
}

// HERE call it with new
export var instance = new AppViewModel();
PS:债券。 詹姆斯债券。

PSPS:不推荐export =