我一直在项目中使用命名空间,这很好,直到突然找不到这个'Test'命名空间。我错过了什么吗?
Can't find variable: Test in Game.ts
example.ts
namespace Test {
export class Example {
constructor() {
console.log("example");
}
}
}
game.ts
/// <reference path="test/example.ts"/>
namespace Game {
export class Snippet {
constructor() {
// Test is not found
let s = new Test.Example();
}
}
}
// Game.Snippet IS found
window.addEventListener("load", () => new Game.Snippet());
更新
仔细观察后,我认为这是我的gulpfile或browserify中的错误。当我使用默认的tsc动作进行编译时,它可以工作!但是当使用browserify时它会失败。我跟着this tutorial
Gulpfile.js
var gulp = require("gulp");
var browserify = require("browserify");
var source = require('vinyl-source-stream');
var tsify = require("tsify");
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var buffer = require('vinyl-buffer');
gulp.task("build", function () {
return browserify({
basedir: '.',
debug: true,
entries: ['dev/main.ts'],
cache: {},
packageCache: {}
})
.plugin(tsify)
.bundle()
.pipe(source('main.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest("docs/js"));
});
答案 0 :(得分:1)
我从您的问题中假设您的文件夹结构如下:
基于此,您可以尝试将参考设置为以下
/// <reference path="./test/example.ts"/>
<强>更新强>
我曾尝试导出名称空间并导入它们。它在离子样本项目中运行良好
<强> example.ts 强>
export namespace Test {
export class Example {
constructor() {
console.log("example");
}
}
}
<强> game.ts 强>
import {Test} from "./test/example"/>
export namespace Game {
export class Snippet {
constructor() {
// Test is not found
let s = new Test.Example();
}
}
}