该代码位于一个名为routes.js的文件中
以下对我不起作用:
var scripts = document.getElementsByTagName("script")
var currentScriptPath = scripts[scripts.length-1].src;
var baseUrl = currentScriptPath.substring(0, currentScriptPath.lastIndexOf('/') + 1);
做到了以下几点:
var bu2 = document.querySelector("script[src$='routes.js']");
currentScriptPath = bu2.src;
baseUrl = currentScriptPath.substring(0, currentScriptPath.lastIndexOf('/') + 1);
我的测试基于以下博客,有关使用require延迟加载angular的方法:http :
//ify.io/lazy-loading-in-angularjs/
require.js产生了requireConfig引导程序
requireConfig生成一个有角度的app.js
有角的app.js生了我的routes.js
我的revel Web框架和asp.net mvc提供了相同的代码。在启示录document.getElementsByTagName(“ script”)中生成了我的require bootstrap js文件的路径,而不是我的route.js。在ASP.NET MVC中,它生成了Visual Studio注入的Browser Link脚本元素的路径,该路径在调试会话期间放置在那里。
这是我的工作routes.js代码:
define([], function()
{
var scripts = document.getElementsByTagName("script");
var currentScriptPath = scripts[scripts.length-1].src;
console.log("currentScriptPath:"+currentScriptPath);
var baseUrl = currentScriptPath.substring(0, currentScriptPath.lastIndexOf('/') + 1);
console.log("baseUrl:"+baseUrl);
var bu2 = document.querySelector("script[src$='routes.js']");
currentScriptPath = bu2.src;
console.log("bu2:"+bu2);
console.log("src:"+bu2.src);
baseUrl = currentScriptPath.substring(0, currentScriptPath.lastIndexOf('/') + 1);
console.log("baseUrl:"+baseUrl);
return {
defaultRoutePath: '/',
routes: {
'/': {
templateUrl: baseUrl + 'views/home.html',
dependencies: [
'controllers/HomeViewController',
'directives/app-style'
]
},
'/about/:person': {
templateUrl: baseUrl + 'views/about.html',
dependencies: [
'controllers/AboutViewController',
'directives/app-color'
]
},
'/contact': {
templateUrl: baseUrl + 'views/contact.html',
dependencies: [
'controllers/ContactViewController',
'directives/app-color',
'directives/app-style'
]
}
}
};
});
从Revel运行时,这是我的控制台输出。
currentScriptPath:http:
baseUrl:http:
bu2:[object HTMLScriptElement] routes.js:13
src:http:
baseUrl:http:
我做的另一件事是利用require config并在其中放入一些自定义配置。即添加
customConfig: { baseRouteUrl: '/AngularLazyBaseLine/Home/Content' }
然后,您可以通过在route.js内部使用以下代码来获取它
var requireConfig = requirejs.s.contexts._.config;
console.log('requireConfig.customConfig.baseRouteUrl:' + requireConfig.customConfig.baseRouteUrl);
有时您需要预先定义一个baseurl,有时您需要动态生成它。您根据情况选择。