发送参数以在 JS 中使用函数使用 API | AEM 6.5

Send arguments to use function in JS Use API | AEM 6.5

我正在尝试在我的 Javascript 文件中获取 url。但似乎 window.location 在 JS 中工作 使用 API 既不。所以我试图将 URL 作为参数发送,但失败并出现错误。 我的 JS 代码:

"use strict";
use(function () {
var url = this.url;
/other code/
});

我的HTML代码:

 <sly data-sly-use.item="'myfile.js' @ url=value">

HTL/Sightly 是一种服务器端模板语言。呈现页面时,将编译脚本(包括 JS 使用对象)并 运行 一次。要获得当前 URL/location,您可以利用 SlingHttpServletRequest#getRequestPathInfo. The current request is available as request as part of the HTL Global Objects.

此外,关于在使用 <sly data-sly-use.item="'myfile.js' @ url=value"> 时出现错误,这很可能是因为 value 在 HTL 服务器端渲染上下文中未定义为变量名。使用 <sly data-sly-use.item="'myfile.js' @ url='https://www.test.com/'"> 应该可以。