在rail 7 中使用带有导入地图的chart.js

问题描述 投票:0回答:2

我正在尝试将

chartjs
与 Rails 7 中的导入映射集成,但它不起作用

我尝试过以下程序

     bin/importmap pin "chart.js@^3.8.0" --download

这是我的application.js

    import 'chart.js'

我在 chrome 控制台中收到以下错误,我相信它与 ES 模块有关

Get http://localhost:5000/_/e09df68b.js` net::ERR_ABORTED 404 (Not Found)    chart.js-67657cw24cb.js:1 

我是否遗漏了某些内容,或者尚无法将 Chart.js 与 Rails 7 集成

ruby-on-rails chart.js jspm import-maps rails7
2个回答
4
投票

我可以通过从 CDN 获取库而不是下载来解决这个问题。为此,我首先取消固定下载的版本:

bin/importmap unpin chart.js --download

然后我没有下载就重新固定了它:

bin/importmap pin chart.js

现在可以了!


0
投票

截至 2023 年 8 月,Chartkick(使用 Chart.js)与 Importmap 的设置在 Chartkick 文档中进行了描述,该文档对我有用。

导入地图
在 config/importmap.rb 中,添加:

pin "chartkick", to: "chartkick.js"
pin "Chart.bundle", to: "Chart.bundle.js"

并在 app/javascript/application.js 中添加:

import "chartkick"
import "Chart.bundle"
© www.soinside.com 2019 - 2024. All rights reserved.