繁体   English   中英

从 Json 文件导入

[英]importing from a Json file

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

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

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

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

您可以序列化对象:

useLottie(JSON.stringify(animation))

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

useLottie({ animationData: animation })

这在README 中有介绍

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM