如何在另一个 class 中创建导入的 class 的实例

How to create an instance of an imported class in another class

User.js

class User {

  constructor() {
    this.fisrtName
    this.lastName
  }

  get fullName() {
    `${this.firtName} ${this.lastName}`
  }

}

export default User

Test.js

import React from 'react'
import {User} from './User'

class Test extends React.Component {

  testClick(e) {
    const user = new User() //error! because User is undefined.
  }

  render() {
    return (
      <button onClick={this.testClick.bind(this)}>test</button>
    )
  }

}

export default Test

改变

import {User} from './User'

import User from './User'

你的错误是你正在 default export: export default User inside User.js 模块,但是在 Test.js 中,您正在制作 named import: import {User} from './User'.