2

我的打字稿文件生成以下输出,但我得到一个 ko 在我的 AppViewModel 中未定义。欢迎任何关于我如何解决这个问题的指示。带有打字稿的视觉工作室解决方案在这里:https ://github.com/s093294/typescript-knockout

/Scripts/App/config.js

require.config({
    baseUrl: '/Scripts/App/',
    paths: {
        'jQuery': '/scripts/jquery-1.9.1',
        'knockout': '/scripts/knockout-2.2.1.debug',
        'AppViewModel': '/Scripts/ViewModels/AppViewModel'
    },
    urlArgs: "bust=" + (new Date()).getTime(),
    shim: {
        jQuery: {
            exports: '$'
        },
        'knockout': {
            deps: [
                "jQuery"
            ],
            exports: 'ko'
        },
        'AppViewModel': {
            deps: [
                'knockout'
            ]
        }
    }
});
require([
    'AppViewModel'
], function (avm) {
    var viewmodel = new avm.AppViewModel();
    ko.applyBindings(viewmodel);
    alert('hello world - SUCCESS');
});

/脚本/视图模型

define(["require", "exports", "knockout"], function(require, exports) { // I would like ko to be in the function handle, but typescript cant do this. But it should be in the global scope also right?
    var AppViewModel = (function () {
        function AppViewModel() {
            this.title = ko.observable();
            this.title('My Sample');
        }
        AppViewModel.prototype.setTitle = function (title) {
            this.title(title);
        };
        return AppViewModel;
    })();
    exports.AppViewModel = AppViewModel;    
})

索引.html

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />    
    <title>Home</title>
</head>
<body>
    Did you get a hello world?
    <div data-bind="text: title"></div>

    <script data-main="/Scripts/App/config" type="text/javascript" src="~/scripts/require.js"></script>

</body>
</html>
4

3 回答 3

3

我通过以下方式破解了它,但如果人们能找到更好的方法,我希望得到任何评论。

require(['knockout','AppViewModel'], (koo,avm) => {
    (<any>window).ko = koo;
    var viewmodel = new avm.AppViewModel();
    ko.applyBindings(viewmodel);
    alert('hello world - SUCCESS');

});
于 2013-04-11T17:08:44.383 回答
1

尝试先加载淘汰赛,看起来您的视图模型是一个已编译的 TypeScript 模块。手动加载淘汰赛而不是依赖项可能会给您更多的控制权。

require([
    'knockout',
    'AppViewModel'
], function (ko, avm) {
    var viewmodel = new avm.AppViewModel();
    ko.applyBindings(viewmodel);
    alert('hello world - SUCCESS');
});

还要确保将淘汰赛作为 AppViewModel 的一个部门删除

于 2013-04-09T16:09:53.600 回答
0

抱歉,对于我的原始答案,但您是否尝试过将 config.js 文件中的 /Scripts/ 引用大写?

于 2013-04-09T15:54:13.063 回答