我目前正在使用next export
创建预渲染的HTML。我面临的问题是我需要在导出之前将现有的标头HTML文件导入到我的组件中。此标头HTML文件也位于与dist文件夹无关的目录中。
因此,假设我的dist文件位于此子目录中:
www.mysite.com/myproject/
我需要导入的标头位于此处:
www.mysite.com/htmls/nav/header.html
在将此header.html
导出为静态html页面之前,是否可以导入并显示此Question #i:
?预先谢谢你!
答案 0 :(得分:0)
有两种方法可以实现您的目标:
使用JavaScript
<script>
function includeHeader() {
var z, i, elmnt, file, xhttp;
/*loop through a collection of all HTML elements:*/
z = document.getElementsByTagName("*");
for (i = 0; i < z.length; i++) {
elmnt = z[i];
/*search for elements with a certain atrribute:*/
file = elmnt.getAttribute("w3-include-html");
if (file) {
/*make an HTTP request using the attribute value as the file name:*/
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {elmnt.innerHTML = this.responseText;}
if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
/*remove the attribute, and call this function once more:*/
elmnt.removeAttribute("w3-include-html");
includeHTML();
}
}
xhttp.open("GET", file, true);
xhttp.send();
/*exit the function:*/
return;
}
}
}
</script>
然后称呼它
<script>
includeHeader();
</script>
使用HTML
<div w3-include-html="/htmls/nav/header.html"></div>