我目前正在使用带有typescript的NextJS,我已经插入了脚本:
<Head><link href="/css/bindingbox.min.css" rel="stylesheet"></link><script async type="text/javascript" src="/scripts/annotorious.min.js"></script></Head>当我试着打电话的时候:
useEffect(()=>{
setTimeout(() => {
var anno = Annotorious.init({
image: "raven_cafe",
readOnly: true,
});
anno.loadAnnotations('/scripts/annotations.w3c.json');
//disable displaying the binding box when selected on the image
anno.on('selectAnnotation', function(data) {
let element = document.querySelector(`g[data-id="`+data.id+`"]`);
element.classList.remove('selected');
});
}, 100);
},[]);我收到一个typescript错误,上面写着:“找不到名字'Annotorious'”如何删除这个错误?
发布于 2021-09-25 09:26:19
我没有太多使用NextJS的经验,但这不会呈现静态页面吗?因此,我猜测这与NextJS在呈现阶段转换输出的方式有关。
如果通过标记导入,Annotorious实际上是一个全局变量:window.Annotorious,因此您可能需要采取一些额外的预防措施才能在NextJS中执行此操作。
这种方法可能会有所帮助:https://medium.com/swlh/how-to-use-a-library-in-next-js-that-wants-window-whatever-96c738263d67
关键的一点似乎是dynamic import的特性,它确保了使用Annotorious的代码只在浏览器中运行,并且在单点登录期间不会被触及。
https://stackoverflow.com/questions/68993573
复制相似问题