2017-08-16 62 views
0

我目前是一個基於preactJS和algolia的小項目嗎?我發現有algolia組分用於反應,但不幸的是,preactjs沒有algolia組分。這就是爲什麼我的問題是如何將algolia JavaScript文件包含到preactjs中?如何在PreactJS中包含外部JavaScript庫?

<script src="https://cdn.jsdelivr.net/instantsearch.js/1/instantsearch.min.js"></script> 
+0

另外,我不知道,如果你看到它,但我們有InstantSearch的另一種風味,做出反應,InstantSearch,這可能會與您的preact應用程序更好地集成。請毫不猶豫地告訴我們您的反饋意見。 – Marie

+0

@Marie我試過'React-InstantSearch',但是當我渲染時,我遇到了'process'錯誤。這就是爲什麼我放棄了。 – ppshein

+0

您能否在我們的github存儲庫中發現您遇到的錯誤?我們需要調查一下:) – Marie

回答

2

您可以動態地插入腳本,試試這個:

componentWillMount() { 
    const script = document.createElement("script"); 
    script.src = "https://cdn.jsdelivr.net/instantsearch.js/1/instantsearch.min.js"; 
    script.async = true; 

    script.onload = function() { 
     // init your algolia code here 
    } 

    document.body.appendChild(script); 
}, 
+0

'var client = algoliasearch('app','key');' 'var index = client.initIndex('my_index');' 'algoliasearch'沒有定義 – ppshein

+0

'''algoliasearch'''只會在加載腳本後定義。將此代碼添加到'''script.onload =()=> {//您的代碼//}'塊。 –

1

您可以再補充它在您的index.html head標籤之間。 在反應過來,index.html的是public/index.html 在Preact,index.html的是src/index.html

<head> 
<script src="https://cdn.jsdelivr.net/instantsearch.js/1/instantsearch.min.js"></script> 
</head> 
+0

在preact-cli中,沒有html文件。只有.js文件。 – ppshein

+0

如何將腳本下載到您的資產並使用'require('../ assets/instantsearch.min.js');'(在index.js中)?也許也嘗試cdn url而不是相對路徑,但我不確定它是否有效。 – Sbe88

+0

此外,這裏有一個與index.html https://github.com/developit/preact-todomvc/blob/master/src/index.html – Sbe88

相關問題