简体   繁体   English

对象销毁 ES6 类

[英]Object destructing ES6 class

How can I properly apply object destructing for the methods in ES6 classes如何为 ES6 类中的方法正确应用对象析构

user.ts用户.ts

import { Request, Response } from "express";

export class User {

  constructor (){
      Object.assign(this,{
          root:this.root,
          get:this.get
      })
  }  
  public  root(req: Request, res: Response) { 
    res.status(200).send({
      message: "DEFAULT request successful!!"
    });
  }
  public get(req: Request, res: Response){
    res.status(200).send({
        message: "USER request  successful!!"
      });
  }

}

export const user = new User();

And i am import like this我是这样进口的

import  {root,get} from './user'

But it will be thrown has no exported member error但是会抛出没有导出成员的错误

UPDATE更新

Based on comments i changed my export to the following根据评论,我将导出更改为以下内容

let obj = new User();

export const user = {
    root:obj.root,
    get:obj.get
}

But still the same error但还是同样的错误

You need to export root and get - destructure your export line:您需要导出rootget - 解构您的export行:

export const { root, get } = new User();

If you also want to export a user:如果您还想导出用户:

export const user = new User();
export const { root, get } = user;

You could also just import user and manually create the functions:您也可以只导入user并手动创建函数:

import { user } from "./user";
const { root, get } = user;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM