在Chrome开发者控制台中通过Require.js访问全局变量 [英] Access global variables from Require.js in the Chrome Developer Console
问题描述
我正在创建一个ASP.NET单页应用程序,并且有一个require.js配置文件,该文件在应用程序启动时运行,引用jQuery,Sammy.js和kickout.js.我为三个第三方库创建了垫片,以允许我在全局级别访问它们:
I am creating an ASP.NET single-page application, and have a require.js config file that runs on application start, referencing jQuery, Sammy.js, and knockout.js. I have created shims for the three third-party libraries to allow me to access them on a global level:
require.config({
paths: {
"jquery": "/Scripts/jquery-2.1.4.min",
"sammy": "/Scripts/sammy-0.7.5.min",
"knockout": "/Scripts/knockout-3.3.0",
"text": "/Scripts/text",
"appVm": "/Scripts/app/appViewModel"
},
shim: {
"jquery": {
exports: "$"
},
"sammy": {
deps: ["jquery"],
exports: "Sammy"
},
"knockout": {
deps: ["jquery"],
exports: "ko"
}
},
priority: ["text", "app"],
});
define(["knockout", "appVm", "sammy"], function(ko, appVm, sammy) {
var vm = new appVm();
ko.applyBindings(vm);
var app = sammy(function() {
this.get("#Dashboard", function() {
//Dashboard-related logic here
});
});
app.run("#Dashboard");
});
我能够实例化我的剔除视图模型并将其绑定到页面.但是,当我尝试访问全局变量"ko"进行敲除以便在Chrome Developer控制台中进行调试时,则未定义任何内容.
I am able to instantiate my knockout viewmodel and bind it to the page. However, when I try to access the global variable "ko" for knockout to debug in the Chrome Developer console, nothing is defined.
如何获取"ko"对象以在Chrome中进行调试?
How can I obtain the "ko" object for debugging in Chrome?
推荐答案
您可以通过以下主要功能将ko对象公开为全局对象:
You can expose the ko object as a global from your main function like this:
define(["knockout", "appVm", "sammy"], function(ko, appVm, sammy) {
// expose ko as global
window.ko = ko;
var vm = new appVm();
ko.applyBindings(vm);
var app = sammy(function() {
this.get("#Dashboard", function() {
//Dashboard-related logic here
});
});
app.run("#Dashboard");
});
另外,您可能想查看框架 DurandalJS ,它是一个非常不错的框架,它使用了requirejs,KnockoutJS和JQuery.它具有自己的路由功能和良好的应用程序生命周期.
Also you might want to check out the framework DurandalJS its a really nice framework that uses requirejs, KnockoutJS and JQuery. It has its own routing capabilities and a nice application lifecycle to work with.
这篇关于在Chrome开发者控制台中通过Require.js访问全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!