我目前是一个基于preactJS和algolia的小项目吗?我发现有algolia组分用于反应,但不幸的是,preactjs没有algolia组分。这就是为什么我的问题是如何将algolia JavaScript文件包含到preactjs中?如何在PreactJS中包含外部JavaScript库?
<script src="https://cdn.jsdelivr.net/instantsearch.js/1/instantsearch.min.js"></script>
我目前是一个基于preactJS和algolia的小项目吗?我发现有algolia组分用于反应,但不幸的是,preactjs没有algolia组分。这就是为什么我的问题是如何将algolia JavaScript文件包含到preactjs中?如何在PreactJS中包含外部JavaScript库?
<script src="https://cdn.jsdelivr.net/instantsearch.js/1/instantsearch.min.js"></script>
您可以动态地插入脚本,试试这个:
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);
},
'var client = algoliasearch('app','key');' 'var index = client.initIndex('my_index');' 'algoliasearch'没有定义 – ppshein
'''algoliasearch'''只会在加载脚本后定义。将此代码添加到'''script.onload =()=> {//您的代码//}'块。 –
您可以再补充它在您的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>
另外,我不知道,如果你看到它,但我们有InstantSearch的另一种风味,做出反应,InstantSearch,这可能会与您的preact应用程序更好地集成。请毫不犹豫地告诉我们您的反馈意见。 – Marie
@Marie我试过'React-InstantSearch',但是当我渲染时,我遇到了'process'错误。这就是为什么我放弃了。 – ppshein
您能否在我们的github存储库中发现您遇到的错误?我们需要调查一下:) – Marie