Javascript 未能加载路由模块requirejs?杜兰达尔虫?
我创建了一个Asp.NETMVC,并使用nuget添加了HotTower(2013年9月11日的V2.0.1)。我创建了两个ViewModel,模型。然而,我得到了以下错误 加载路由模块(viewmodels/myVM)失败。详细信息:模块的加载超时:durandal/plugins/router\nhttp://requirejs.org/docs/errors.html#timeout" 这是durandal/插件/路由器的问题吗?或者它可能是由我添加的一些代码引起的 错误发生在Scripts/durandal/system.jsJavascript 未能加载路由模块requirejs?杜兰达尔虫?,javascript,asp.net-mvc,requirejs,durandal,hottowel,Javascript,Asp.net Mvc,Requirejs,Durandal,Hottowel,我创建了一个Asp.NETMVC,并使用nuget添加了HotTower(2013年9月11日的V2.0.1)。我创建了两个ViewModel,模型。然而,我得到了以下错误 加载路由模块(viewmodels/myVM)失败。详细信息:模块的加载超时:durandal/plugins/router\nhttp://requirejs.org/docs/errors.html#timeout" 这是durandal/插件/路由器的问题吗?或者它可能是由我添加的一些代码引起的 错误发生在Script
var logError = function(error) {
if(error instanceof Error){
throw error;
}
throw new Error(error);
};
acquire: function() {
var modules,
first = arguments[0],
arrayRequest = false;
if(system.isArray(first)){
modules = first;
arrayRequest = true;
}else{
modules = slice.call(arguments, 0);
}
return this.defer(function(dfd) {
require(modules, function() {
var args = arguments;
setTimeout(function() {
if(args.length > 1 || arrayRequest){
dfd.resolve(slice.call(args, 0));
}else{
dfd.resolve(args[0]);
}
}, 1);
}, function(err){
dfd.reject(err);
});
}).promise();
},
下面是VM代码
define(['services/datacontext', 'durandal/plugins/router', 'services/logger'],
// Remove the durandal/plugins/router and the functions will get rid of the error.
function (datacontext, router, logger) {
var title = 'Event';
var vm = {
activate: activate,
deactivate: deactivate,
refresh: refresh,
events: events,
title: title
};
return vm;
//#region Internal Methods
var events = ko.observableArray();
function activate() {
logger.log(title + ' View Activated', null, title, true);
return datacontext.getEventPartials(events);
}
var deactivate = function () {
events([]);
};
var refresh = function () {
return datacontext.getEventPartials(events, true);
};
//#endregion
});
下面是调用堆栈
logError [system.js] Line 92 Script
Anonymous function [router.js] Line 359 Script
[External Code]
Anonymous function [system.js] Line 260 Script
[External Code]
[Async Call]
....
代码位于router.js
isProcessing(true);
router.activeInstruction(instruction);
if (canReuseCurrentActivation(instruction)) {
ensureActivation(activator.create(), currentActivation, instruction);
} else {
system.acquire(instruction.config.moduleId).then(function(module) {
var instance = system.resolveObject(module);
ensureActivation(activeItem, instance, instruction);
}).fail(function(err){
system.error('Failed to load routed module (' + instruction.config.moduleId + '). Details: ' + err.message);
});
}
}
和system.js中的上一个
var logError = function(error) {
if(error instanceof Error){
throw error;
}
throw new Error(error);
};
acquire: function() {
var modules,
first = arguments[0],
arrayRequest = false;
if(system.isArray(first)){
modules = first;
arrayRequest = true;
}else{
modules = slice.call(arguments, 0);
}
return this.defer(function(dfd) {
require(modules, function() {
var args = arguments;
setTimeout(function() {
if(args.length > 1 || arrayRequest){
dfd.resolve(slice.call(args, 0));
}else{
dfd.resolve(args[0]);
}
}, 1);
}, function(err){
dfd.reject(err);
});
}).promise();
},
根据评论,我建议稍微修改vm代码,以便在使用之前定义通过
vm
返回的所有变量。此外,使用'plugins/router'
代替'durandal/plugins/router'
define(['services/datacontext', 'plugins/router', 'services/logger'],
// Remove the durandal/plugins/router and the functions will get rid of the error.
function (datacontext, router, logger) {
var title = 'Event';
var events = ko.observableArray();
var deactivate = function () {
events([]);
};
var refresh = function () {
return datacontext.getEventPartials(events, true);
};
var vm = {
activate: activate,
deactivate: deactivate,
refresh: refresh,
events: events,
title: title
};
return vm;
//#region Internal Methods
function activate() {
logger.log(title + ' View Activated', null, title, true);
return datacontext.getEventPartials(events);
}
//#endregion
});
顺便说一句,Internal methods的名称有误导性,因为该区域中的所有内容都是通过vm
返回的。我更喜欢使用命名函数,如果返回,则在return语句之前创建命名函数,如果未返回,则将其放置在内部方法区域中return语句的下面
define(['services/datacontext', 'plugins/router', 'services/logger'],
function( datacontext, router, logger ) {
var title = 'Event';
var events = ko.observableArray();
function deactivate () {
events([]);
}
function refresh () {
return datacontext.getEventPartials(events, true);
}
function activate () {
logger.log(title + ' View Activated', null, title, true);
return datacontext.getEventPartials(events);
}
return {
activate: activate,
deactivate: deactivate,
refresh: refresh,
events: events,
title: title
};
//#region Internal Methods
//#endregion
});
require.config.path
的外观如何?在2.0.x中,到路由器的路径通常是plugins/router
。此外,将下面定义的所有变量移动到上面的return vm
var vm={…,否则在使用之前不会定义。main.js中的require.config是require.config({path:{'text':'../Scripts/text','durandal':'../Scripts/durandal','plugins':'../Scripts/durandal/plugins','transitions':'../Scripts/durandal/transitions'}})
尝试在VM代码中将'durandal/plugins/router'
替换为'plugins/router'
。太好了。从require参数中删除durandal/
后它就可以工作了。非常感谢。