简体   繁体   English

参考错误:在带有 webpacker 的新 rails6 应用程序中找不到变量:d3

[英]ReferenceError: Can't find variable: d3 in new rails6 application with webpacker

I am working on a personal project with rails 6 and d3js.我正在使用 rails 6 和 d3js 进行个人项目。 In fact trying to prepare configuration to start :-)实际上正在尝试准备配置以启动:-)

I used yarn add d3js@4.13.0 -T to install d3js and appended line我使用yarn add d3js@4.13.0 -T来安装 d3js 和附加行

import * as d3 from "d3";

to my javascript/packs/application.js到我的javascript/packs/application.js

My problem is I am not able to access d3 in the console.我的问题是我无法在控制台中访问 d3。 Webpacker is not in my comfort zone yet! Webpacker 还没有进入我的舒适区!

Webpack does not export anything to the global scope by default.默认情况下,Webpack 不会将任何内容导出到全局范围。 Anything you import within your Webpack bundles is within the local scope of the module represented by the file you're working in.您在 Webpack 包中导入的任何内容都在您正在处理的文件所表示的模块的本地范围内。

To make a variable available to the global scope, you have to expose it somehow.要使变量可用于全局范围,您必须以某种方式公开它。 You could configure something like expose-loader to do this.您可以配置类似expose-loader类的东西来执行此操作。 You can instead assign the variable to the global scope, ie, window, after importing it:您可以在导入后将该变量分配给全局范围,即窗口:

// app/javascript/packs/application.js
import * as d3 from "d3"
window.d3 = d3

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

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