我正在将一个项目从 CQ 5.6.1 迁移到 AEM 6.1。
然后,当我测试它时,发现我的代码中使用 typeahead.bundle.js 的部分不再工作。当我返回在 CQ 5.6.1 中测试该项目时,它确实工作正常。
尝试隔离问题(获取代码片段,无需将其部署到 AEM 6.1),并且工作正常。
我的初步发现是,单独的 AEM 6.1 服务器可能会导致 typeahead.bundle.js 无法工作。
<html>
<head>
<script src="jquery-2.0.0.min.js"></script>
<script src="hogan-3.0.1.js"></script>
<script src="typeahead.bundle.js"></script>
</head>
<body>
<row>
<input id="country-search" type="text" class="typeahead form-control" placeholder="Country" title="Enter the terms you wish to search for" />
</row>
<script type="text/javascript">
$(document).ready(function() {
var countries = new Bloodhound({
name: 'countrysearch-countries',
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('englishName', 'localName'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 5,
prefetch: {
url: 'country.json',
}
});
var countrySuggestionTemplate = Hogan.compile('<p>{{localName}} ({{englishName}})</p>');
countries.initialize();
$("#country-search").typeahead(null, {
displayKey: 'localName',
source: countries.ttAdapter(),
templates: {
suggestion: function(country) {
return countrySuggestionTemplate.render(country);
}
}
}).on('typeahead:selected', function (e, datum) {
window.location = datum.url;
});
});
</script>
country.json 的 JSON 字符串内容:
[{"url":"http://xxxx.com ","localName":"阿富汗","englishName":"阿富汗"},{"url":"http://xxxx.com ","localName":"Shqipëria","englishName":"阿尔巴尼亚"},{"url":"http://xxxx.com ","localName":"阿尔巴尼亚","englishName":" Algolia "}]
注意:请注意,上面的 HTML 脚本在未部署到 AEM 6.1 时可以正常工作。
希望大家能帮我重新评分。
谢谢
最佳答案
我已经找到答案了。
由于未声明的变量,aem6 服务器中似乎存在未初始化的内部 JavaScript(很奇怪,因为它在 CQ 5.6.1 中工作)。
所以我明确地声明了该变量,现在它工作正常。
关于javascript - Typeahead 在 AEM 6.1 中不起作用,但在 CQ 5.6.1 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32988441/