2
Node7.4.0/ES6 /活字体2.1.5/WebStorm行でES6 *活字体:見つけることができません名前空間
2016.3: 輸出デフォルトheroRoutes.router。
は私が取得:TS2503は それと間違って何ができるか、それとのinit() を作成した後、名前空間「heroRoutes」を見つけることができませんか?
フィードバックに感謝します。この理由は、あなたが修飾名をエクスポートすることはできませんということです
HeroRouter.ts
import {Router, Request, Response, NextFunction} from 'express';
const Heroes = require('../data');
export class HeroRouter {
router: Router;
/**
* Initialize the HeroRouter
*/
constructor() {
this.router = Router();
this.init();
}
/**
* GET all Heroes.
*/
public getAll(req: Request, res: Response, next: NextFunction) {
res.send(Heroes);
}
/**
* GET one hero by id
*/
public getOne(req: Request, res: Response, next: NextFunction) {
let query = parseInt(req.params.id);
let hero = Heroes.find(hero => hero.id === query);
if (hero) {
res.status(200)
.send({
message: 'Success',
status: res.status,
hero
});
}
else {
res.status(404)
.send({
message: 'No hero found with the given id.',
status: res.status
});
}
}
/**
* Take each handler, and attach to one of the Express.Router's
* endpoints.
*/
init() {
this.router.get('/', this.getAll);
this.router.get('/:id', this.getOne);
}
}
// Create the HeroRouter, and export its configured Express.Router
let heroRoutes = new HeroRouter();
heroRoutes.init();
export default heroRoutes.router;
ありがとうAluan、それがポイントです!また、2つのconst宣言の間にheroRouter.init()を挿入することを忘れないでください。 ここにポイントがありますか?それはエクスポートのデフォルトのパラメータにありますか... 何らかのドキュメントへのリンクは... ... – erwin
確かにコンテキストがありません。回答が更新されました –
'init'メソッドに関しては、コンストラクタ内のすべてのロジック。オブジェクトを無効な状態で作成できるようにすることは、悪い習慣です。あなたの質問の文脈で私の例のようなバグにつながります。 –