提问人:user3523406 提问时间:5/29/2018 更新时间:5/29/2018 访问量:504
打字稿:未捕获的 ReferenceError:未定义 Morph
Typescript: Uncaught ReferenceError: Morph is not defined
问:
我收到以下错误:
Uncaught ReferenceError: Morph is not defined
at sketch (sketch.ts:7)
at new p5 (sketch.ts:28)
at Object.<anonymous> (sketch.ts:28)
at c (sketch.ts:28)
at Function.r.import (sketch.ts:28)
at sketch.ts:28
morph.ts位于同一文件夹中,这是sketch.ts:sketch.ts
import 'p5'
import './morph'
var sketch = (p: p5) => {
const morph = new Morph();
p.preload = () => {
}
p.setup = () => {
p.createCanvas(p.windowWidth, p.windowHeight);
morph.setup(p);
}
p.windowResized = () => {
p.resizeCanvas(p.windowWidth, p.windowHeight);
}
p.draw = () => {
p.background(100);
morph.draw(p);
}
}
var sketchP = new p5(sketch);
为什么导入morph.ts不起作用。我错过了什么?
先谢谢你。
答:
1赞
Chris Edgington
5/29/2018
#1
您必须将所有内容作为命名空间、默认导出或特定导出导入。例如,由于您的代码指示 Morph 是一个类,因此应将其标记为导出类或默认导出。我宁愿不使用默认导出,所以我会确保 Morph 在你声明它的地方导出:
export class Morph {
然后在sketch.ts中执行以下操作以导入 Morph 类:
import { Morph } from './morph'
评论
0赞
Chris Edgington
5/30/2018
那么,对于这两个文件,您会得到完全相同的原始错误吗?
0赞
user3523406
5/31/2018
对不起,我得到了一个不同的:Uncaught TypeError: morph_1.Morph is not a constructor at sketch (sketch.ts:7) at new p5 (sketch.ts:28) at Object.<anonymous> (sketch.ts:28) at c (sketch.ts:28) at Function.r.import (sketch.ts:28) at sketch.ts:28 at sketch.ts:28
0赞
Chris Edgington
5/31/2018
目前尚不清楚你想在sketch.ts做什么。您正在定义一个名为 sketch 的函数对象,然后将该函数传递给 p5 的构造函数。你为什么要这么做?
0赞
user3523406
5/31/2018
我尝试用打字稿学习p5.js,并以这个项目为基础。目前我不知道为什么代码是这样写的。
评论