javascript - 来自两个不同 JS 文件的交叉引用数据
问题描述
在main.js
我声明一个画布上下文变量c
。
import { Boid } from './boid';
const canvas = document.querySelector('canvas');
const c = canvas.getContext('2d');
在boid.js
我定义了一个对对象Boid
执行方法的类c
main.js
export class Boid {
constructor(pos, vel, accel) {
this.pos = pos
this.vel = vel
this.accel = accel;
}
draw() {
c.beginPath();
}
}
当我在 chrome 中查看运行站点时,控制台日志
boid.js:8 Uncaught ReferenceError: c is not defined
at e.draw (boid.js:8)
at t (main.js:23)
at main.js:29
at main.js:29
显然,模块化无法正常工作。我的问题是如何使用 in 中的变量和 from中boid.js
的main.js
变量,最好不必分别在它们中导入/导出。main.js
boid.js
我将 Rollup 用作捆绑程序,因此仅在 index.html 中链接一个缩小的 js 文件,因此不能将 boid.js 链接到 main.js 之上。
你可以在https://github.com/michaelmoreno/boids-algorithm自己查看我的仓库
解决方案
用这个:
window.c = canvas.getContext('2d')
然后:
window.c.beginPath()
推荐阅读
- database - 使用 HTTP 响应实现 Dialogflow
- python - 如何将 Keras 预测输出转换为所需的二进制值
- java - 如何在不向 Src 添加文件的情况下将文件放入 GitLab 测试
- c# - Wavecom 调制解调器 stk 的每个功能第一次都能完美运行,而不是其他时间。使用 AT 命令和 C#
- sql - 从触发器调用过程以验证生日
- ios - 如何获取 iOS BSSID
- python - 在课堂上作为参数列出?
- javascript - 如何将 JSON 数据存储在 html 标签中?
- html - HTML - 如何使用 thead/tfoot 打印页码
- android - Android studio - 没有连接设备的问题