2 回答

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
這是因?yàn)橥獠?css 文件被解釋為文本而不是 css 文件
The resource from “https://gist.githubusercontent.com/eirikbakke/1059266/raw/d81dba46c76169c2b253de0baed790677883c221/gistfile1.css” was blocked due to MIME type (“text/plain”) mismatch (X-Content-Type-Options: nosniff

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
我認(rèn)為 GitHub 不允許在其他地方使用其原始托管文件,因此 Github 已阻止來(lái)自跨域的請(qǐng)求。
這是服務(wù)器端配置,您無(wú)法在 Github 服務(wù)器上啟用 CORS。
另一種方法是將文件托管在某些 CDN 中或下載文件并將其保存在本地。
我發(fā)現(xiàn)的另一種選擇是通過(guò) JS 下載文件的內(nèi)容,然后將其附加為樣式元素。
我從未在 React 中工作過(guò),但我相信您更清楚保存下載文件功能的最佳位置。
function loadCssDynamically(fileUrl){
? ? // read text from URL location
? ? var request = new XMLHttpRequest();
? ? request.open('GET', fileUrl, true);
? ? request.send(null);
? ? request.onreadystatechange = function () {
? ? ? ? if (request.readyState === 4 && request.status === 200) {
? ? ? ? ? ? var type = request.getResponseHeader('Content-Type');
? ? ? ? ? ? if (type.indexOf("text") !== 1) {
? ? ? ? ? ? ? ?var style = document.createElement('style');
? ? ? ? ? ? ? ?style.innerHTML = request.responseText;
? ? ? ? ? ? ? ?document.head.appendChild(style);
? ? ? ? ? ? ? ?// console.log(request.responseText);
? ? ? ? ? ? ? ?// return request.responseText;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
var file = "https://gist.githubusercontent.com/eirikbakke/1059266/raw/d81dba46c76169c2b253de0baed790677883c221/gistfile1.css";
loadCssDynamically(file);

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊
重要提示:rawgit.com 即將關(guān)閉。在這里閱讀有關(guān)其他替代方案的更多信息 - https://rawgit.com/
因此,這與您的代碼無(wú)關(guān)。另一種方法是復(fù)制要點(diǎn) url 并將其粘貼到https://raw.githack.com/中,它將提供可用于運(yùn)行示例的鏈接。
我使用你的鏈接來(lái)獲取此鏈接,https://gist.githack.com/eirikbakke/1059266/raw/d81dba46c76169c2b253de0baed790677883c221/gistfile1.css
在你的 css 中應(yīng)用此鏈接可以工作。
工作沙箱 [https://codesandbox.io/s/old-paper-lkdse]
添加回答
舉報(bào)