我在 Angular 和 createjs-module 方面遇到问题。
createjs-module 正在工作,正如您在代码中看到的那样,形状方法和补间正在工作(我可以在浏览器上成功地可视化它):
https://www.npmjs.com/package/createjs-module
但是当我尝试使用 createjs 的官方文档开始预加载和使用图像时,没有任何反应:
http://createjs.com/docs/preloadjs/classes/LoadQueue.html
队列未加载。我将控制台日志放在确实有效的“ngOnInit”末尾,但没有从“queue”对象调度任何事件。 我在代码上没有看到任何错误,在控制台上也没有看到任何错误/警告。
代码:
import { Component, OnInit, ViewChild } from '@angular/core';
import * as createjs from 'createjs-module';
@Component({
selector: 'app-mycomp',
templateUrl: './mycomp.component.html',
styleUrls: ['./mycomp.component.css']
})
export class MyClass implements OnInit {
public stage;
public queue;
public queueManifest = [
{id:"header", src:"header.png"},
{id:"body", src:"body.png"},
{id:"footer", src:"footer.png"}
];
constructor() { }
ngOnInit() {
///THIS CODE WORKS!
this.stage = new createjs.Stage("myCanvas");
createjs.Ticker.setFPS(60);
createjs.Ticker.addEventListener("tick", this.stage);
var circle = new createjs.Shape();
circle.graphics.beginFill("DeepSkyBlue").drawCircle(0, 0, 50);
circle.x = 10;
circle.y = 10;
this.stage.addChild(circle);
createjs.Tween.get(circle, { loop: true })
.to({ x: 400 }, 1000, createjs.Ease.getPowInOut(4))
.to({ alpha: 0, y: 175 }, 500, createjs.Ease.getPowInOut(2))
.to({ alpha: 0, y: 225 }, 100)
.to({ alpha: 1, y: 200 }, 500, createjs.Ease.getPowInOut(2))
.to({ x: 100 }, 800, createjs.Ease.getPowInOut(2));
this.stage.update();
///THIS CODE DOES NOT WORK!
this.queue = new createjs.LoadQueue();
this.queue.on("progress", this.queueProgress, this);
this.queue.on("complete", this.queueComplete, this);
this.queue.on("error", this.queueError, this);
this.queue.loadManifest(this.queueManifest);
///THIS LINE IS ON CONSOLE!
console.log("queue START");
}
///NONE OF THIS IS DISPATCHING
public queueProgress()
{
console.log("queue progress");
}
public queueError()
{
console.log("queue error");
}
public queueComplete()
{
console.log("queue finished");
}
}
最佳答案
您是否尝试在组件的构造函数中将 createJs
声明为 window 对象的对象?
我在使用 Angular CLI 的 Angular 2 项目上预加载 js 时遇到了一些问题,我发现在预加载过程中,一个名为 _isCanceled 的方法尝试使用 窗口进行处理。 createjs 对象,因此,整个事件过程总是被停止。但在我的项目中,createjs 对象没有声明为窗口的对象。所以,我尝试了这个:
constructor()
{
(<any>window).createjs = createjs;
this.queue = new createjs.LoadQueue();
}
关于angular - PreloadJS 不适用于 Angular(createjs-module),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44828676/