Vue.js报错:Unexpected token < in JSON at position 0 求解
修复Lottie-Web配置参数的错误
嘿,我一眼就发现问题所在啦!你在animationParams里把loop和autoplay设置成了字符串类型的'true'和'false',但Lottie-Web的这两个配置项需要的是布尔值(不带引号的true/false),这就是导致错误的核心原因。
修复后的代码
import Lottie from './../../node_modules/lottie-web/build/player/lottie'; export default { name: 'Illustration', mounted() { this.animationParams = { container: document.getElementById('animation'), renderer: 'svg', loop: true, // 去掉引号,使用布尔值 autoplay: false, // 去掉引号,使用布尔值 path: '/sr...' // 确保这里是正确的Lottie动画JSON文件路径 } // 别忘了添加动画初始化代码哦! Lottie.loadAnimation(this.animationParams); } }
额外小提醒
- 确认
container对应的DOM元素(id为animation的元素)在组件挂载时已经存在,不然document.getElementById会返回null - 保证
path指向的是正确的Lottie动画JSON文件路径,路径错误也会导致动画无法加载
内容的提问来源于stack exchange,提问作者Carlos Pisarello
相关产品推荐
相关产品推荐

