从 Json 文件导入

importing from a Json file

我正在尝试从 Json 文件导入,并将生成的导入传递给接受字符串的函数。在代码中我正在做类似的事情:

import animation from "assets\lottie\animation.json"
const LottieRef = useLottie(animation);

其中 useLottie: (path: string) => HTMLDivElement | null 。但是我得到的错误基本上是说动画不是字符串类型。我该怎么办?

乍一看,您的 import 返回 animation AS A JavaScript 从 JSON 的内容解析的对象“...animation.json”。它已准备好使用原样。所以,animation 是一个对象,而不是字符串。

您可以序列化对象:

useLottie(JSON.stringify(animation))

或者更好的是,您可以将其作为动画数据传入:

useLottie({ animationData: animation })

README.

中对此进行了介绍