ES6 export extended class 然后导入
ES6 export extended class and then import it
我有基本的class (mobile.js)
class Mobile {
constructor() {
...
}
method(msg){
...
}
}
module.exports = Mobile;
然后我导入到(mobileextended.js);
import Mobile from './mobile';
class MobilePhone extends Mobile {
method(){
super.method('hello world!');
}
}
module.exports = MobilePhone;
最后我想将它导入到 mobilephone.js:
import MobilePhone from './mobileextended.js';
MobilePhone.method();
我怎样才能使它以 ES6 风格工作?因为现在我收到 Cannot read 属性 'open' of undefined error.
如果你想在导入的地方给东西命名,比如
import Mobile from './Mobile'
您应该使用默认导出,例如
export default Mobile
从定义Mobile
的地方开始。
将此与命名导出进行比较
export class Mobile { ... }
并在单独的模块中导入该特定名称。
import {Mobile} from Mobile
然而,正如loganfsmyth在评论中所指出的,这不是您错误的根源。要使用非静态方法,您需要创建该 class.
的实例
const mobilePhone = new MobilePhone()
mobilePhone.method()
我有基本的class (mobile.js)
class Mobile {
constructor() {
...
}
method(msg){
...
}
}
module.exports = Mobile;
然后我导入到(mobileextended.js);
import Mobile from './mobile';
class MobilePhone extends Mobile {
method(){
super.method('hello world!');
}
}
module.exports = MobilePhone;
最后我想将它导入到 mobilephone.js:
import MobilePhone from './mobileextended.js';
MobilePhone.method();
我怎样才能使它以 ES6 风格工作?因为现在我收到 Cannot read 属性 'open' of undefined error.
如果你想在导入的地方给东西命名,比如
import Mobile from './Mobile'
您应该使用默认导出,例如
export default Mobile
从定义Mobile
的地方开始。
将此与命名导出进行比较
export class Mobile { ... }
并在单独的模块中导入该特定名称。
import {Mobile} from Mobile
然而,正如loganfsmyth在评论中所指出的,这不是您错误的根源。要使用非静态方法,您需要创建该 class.
的实例const mobilePhone = new MobilePhone()
mobilePhone.method()