ES6 * Typescript:找不到命名空间 [英] ES6 * Typescript : Cannot find namespace

查看:1927
本文介绍了ES6 * Typescript:找不到命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node7.4.0 / ES6 / Typescript 2.1.5 / WebStorm 2016.3

Node7.4.0 / ES6 / Typescript 2.1.5 / WebStorm 2016.3

在行上:
export default heroRoutes.router; strong>

On the line : export default heroRoutes.router;

我得到: TS2503创建命令空间'heroRoutes'
后,init()
什么可能是错误的?

I get: TS2503 Cannot find namespace 'heroRoutes' after creating it and init() what could be wrong with it ?

感谢您的反馈


HeroRouter.ts

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;


推荐答案

const heroRouter = new HeroRouter();
const router = heroRouter.router;
export default router;

原因是您无法导出限定名称。
模块的导出被绑定到称为模块命名空间对象的特殊对象。一个原因是,如果合格的出口是合法的,则语义将会令人惊讶,因为更新变量 heroRouter 路由器的值c $ c>不会更新导出绑定的值(这里命名为 default )。

The reason for this is that you cannot export a qualified name. The exports of a module are bound to a special object known as the module namespace object. One reason is that if qualified exports were legal, the semantics would be surprising as updating the value of the instance member router of the variable heroRouter would not update the value of the exported binding (here named default).

这篇关于ES6 * Typescript:找不到命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆