在维护多租户应用程序时,我有几个“住所”模块,具体取决于租户。旧版代码使用PLATFORM.moduleName以及每个租户的硬编码值(在一系列if语句中)。 将其重写为具有来自JSON的路由数组时,会发生错误。
我已经确认硬编码相同的值将起作用。 我检查了JSON文件中的数据是否正确加载,因为它是字符串,并且可以正确显示到控制台日志中。 但是,当将这些参数传递给PLATFORM.moduleName时,它无法识别它们。
interface LocationConfig
{
routes: HomeRoute[];
}
interface HomeRoute
{
id: string;
route: string;
}
let homeLocation: LocationConfig = require('../home-location.json');
this.chosenHome = PLATFORM.moduleName("./home/home.html");
let siteInfo:any = fooService.getSiteInfo();
if (siteInfo && homeLocation)
{
for (let route of homeLocation.routes)
{
// Log added just to check if route is correct.
log.debug("Checking route: " + route.id + " " + route.route);
if (route.id == siteInfo.host)
{
this.chosenHome = PLATFORM.moduleName(route.route);
//Following line would work:
// this.chosenHome = PLATFORM.moduleName("./others/fooUser/pages/home.html");
}
}
}
日志将显示:
> INFO [insight] Checking route: default ./home/home.html
> INFO [insight] Checking route: foo.pro ./others/fooUser/pages/home.html
> INFO [insight] Checking route: localhost ./others/fooUser/pages/home.html
> INFO [insight] Checking route: bar.pro ./others/fooUser/pages/home.html
但是,如果通过route.route,则会出现错误
Unhandled rejection Error: Unable to find module with ID: others/fooUser/pages/home.html
如果我对位置进行硬编码(如注释中所示),则一切正常:
DEBUG [templating] importing resources for others/fooUser/pages/home.html
我对打字稿有些陌生。