我对Typescript相对较新(我已经尝试使用它多年但从未真正理解某些方面)。这个问题一直困扰着我与NPM进口有关的一段时间。我试图在Typescript中使用一些Web NPM模块用于dashjs和chartjs。尝试导入它们然后从转轮脚本中引用它们时都不起作用。
程序文件似乎看不到类SensedPlayback,暗示某些东西没有正确编译,但是SensedPlayback构建的JS文件。删除SensedPlayback.ts文件顶部的import
允许程序文件查看它,但导入的模块不起作用。模块的intellisense工作在SensedPlayback.ts
内,表明模块导入工作正常。
任何帮助将不胜感激! 杰拉德
{
"compileOnSave": true,
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"moduleResolution": "node",
"module": "commonjs",
"target": "es6",
"outDir": "wwwroot/js/"
},
"exclude": [
"node_modules",
"wwwroot"
]
}
import * as dashjs from "dashjs";
namespace TIMINGSRC {
export declare var TimingObject: any;
export declare var MediaSync: any;
}
class SensedPlayback {
production: Production;
videosDiv: HTMLDivElement;
videos: dashjs.MediaPlayerClass[];
seekHead: HTMLInputElement;
playBtn: HTMLButtonElement;
pauseBtn: HTMLButtonElement;
_playbackTimeReciever: PlaybackTimeSync;
_timingObject: any;
_isSeeking: boolean;
constructor(production: Production, videosDiv: HTMLDivElement, seekHead: HTMLInputElement, playBtn: HTMLButtonElement, pauseBtn: HTMLButtonElement) {
this.production = production;
this.videosDiv = videosDiv;
this.seekHead = seekHead;
this.playBtn = playBtn;
this.pauseBtn = pauseBtn;
this._setupPage();
this._playbackTimeReciever = new PlaybackTimeSync();
}
start() {
this._playbackTimeReciever.subscribe((tr) => {
this.loadMediaForTime(tr.start);
});
this._playbackTimeReciever.start();
}
_setupPage() {
this.playBtn.addEventListener('click', (event) => {
this._timingObject.update({ velocity: 1.0 });
});
this.pauseBtn.addEventListener('click', (event) => {
this._timingObject.update({ velocity: 0.0 });
});
this.seekHead.addEventListener('mousedown', (event) => {
this._isSeeking = true;
});
this.seekHead.addEventListener('mouseup', (event) => {
this._timingObject.update({ position: this.seekHead.value });
this._isSeeking = false;
});
}
loadMediaForTime(time: Date) {
let takes = this.production.takes.filter((t) => {
t.media.some((m) => m.startTime >= time
&& new Date(m.startTime.setMilliseconds(m.startTime.getMilliseconds() + m.duration)) < time)
});
let take = takes[0];
this.videos = [];
this.videosDiv.innerHTML = "";
for (var m of take.media) {
let video = <HTMLVideoElement>(document.createElement("video"));
var player = new dashjs.MediaPlayerClass();
player.initialize(video, URL.createObjectURL(`${m.url}/playback.mpd`), false);
let wrapper = <HTMLDivElement>(document.createElement("div"));
wrapper.classList.add("col-md-4");
wrapper.classList.add("col-sm-6");
let heading = <HTMLHeadingElement>(document.createElement("h4"));
heading.innerText = this.production.cameras.find((c) => { return c.id == m.cameraId }).name;
wrapper.appendChild(heading);
wrapper.appendChild(video);
this.videosDiv.appendChild(wrapper);
this.videos.push(player);
}
this._configurePlayers();
}
_configurePlayers() {
for (var v of this.videos) {
v.on('loadstart', function (event) {
v.setMaxAllowedBitrateFor('video', 4600);
});
v.on('loadedmetadata', (event) => {
if (this._timingObject === undefined) {
this._timingObject = new TIMINGSRC.TimingObject({ range: [0, event.target.duration] });
this.seekHead.min = '0';
this.seekHead.step = '0.25';
this.seekHead.max = `${v.duration()}`;
this._timingObject.on('timeupdate', () => {
if (!this._isSeeking) {
this.seekHead.value = this._timingObject.clock.now();
}
});
}
var sync = new TIMINGSRC.MediaSync(event.target, this._timingObject);
});
}
}
}
let videosDiv = <HTMLDivElement>document.getElementById('cameras');
let seekHead = <HTMLInputElement>document.getElementById('seekhead');
let playBtn = <HTMLButtonElement>document.getElementById('play');
let pauseBtn = <HTMLButtonElement>document.getElementById('pause');
let production : Production = JSON.parse((<HTMLInputElement>document.getElementById('production')).value);
let playback = new SensedPlayback(production, videosDiv, seekHead, playBtn, pauseBtn);
playback.start();
答案 0 :(得分:2)
编译一系列不使用import
或export
的文件时,文件被视为不是模块,并且它们声明的所有符号最终都在全局空间中。这就是为什么当您删除import
语句时,SensedPlaybackProgram.ts
能够看到SensedPlayback
,即使它已在SensedPlayback.ts
中定义
在import
中添加SensedPlayback.ts
语句时,将其转换为TypeScript模块,然后再打开,如果要使用它定义的符号,则需要将其从此处导出模块,并导入要使用它的符号。因此,请将SensedPlayback.ts
修改为:
export class SensedPlayback {
...
在SensedPlaybackProgram.ts
中,将其导入:
import { SensedPlayback } from "./SensedPlayback";
(根据需要调整路径。)