为了账号安全,请及时绑定邮箱和手机立即绑定

如何解决 SyntaxError: Unexpected token = 导入类后?

如何解决 SyntaxError: Unexpected token = 导入类后?

繁花如伊 2021-09-17 12:53:55
我在单独的 JavaScript 文件中创建了一个类,并将其导入到另一个文件中。但是,我在导出的类中遇到语法错误。该类在未导出时不会显示任何错误。在Property.js(导出的类)中class Property{    //constants declarations    YEAR_AMOUNT = 10;    PERIOD_AMOUNT = 120;    //Properties    closingCosts;    initialInvestment;    //arrays declarations    //income    adjustedRent= new Array();    adjustedOtherRevenue = new Array();    grossRevenue= new Array();    effectiveRevenue= new Array();constructor(title, address, askPrice, muniLandEval, muniBuildEval, notes){ this.title=title;        this.address=address;    this.askPrice=askPrice;    this.muniLandVal = muniLandEval;    this.muniBuildVal = muniBuildEval;    this.notes=notes}//other methods}export default class Property在propertyManager.js中(导入)//handle property (calculations, creation, save)const express = require('express');const router  = express.Router();const Property = require('./Property.js');router.post('/', (req, res) => {    //create a Property    const property = new Property(req.body.title,        req.body.address,        req.body.askPrice,        req.body.muniLandVal,        req.body.muniBuildVal,        req.body.notes    );})module.exports = router;有问题的错误D:\ImmoTuul\RE_Analysis_\RE_MVP\server\routes\api\Property.js:4    YEAR_AMOUNT = 10;                ^SyntaxError: Unexpected token =    at Module._compile (internal/modules/cjs/loader.js:723:23)    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)    at Module.load (internal/modules/cjs/loader.js:653:32)    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)    at Function.Module._load (internal/modules/cjs/loader.js:585:3)    at Module.require (internal/modules/cjs/loader.js:692:17)    at require (internal/modules/cjs/helpers.js:25:18)    at Object.<anonymous> (D:\ImmoTuul\RE_Analysis_\RE_MVP\server\routes\api\propertyManager.js:4:18)    at Module._compile (internal/modules/cjs/loader.js:778:30)    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)我希望能够在propertyManager文件中使用该类及其构造函数。
查看完整描述

2 回答

?
德玛西亚99

TA贡献1770条经验 获得超3个赞

如果您发布的内容正是您的 Property.js 文件,则看起来您有一个额外的class关键字。

export default class Property; /// 这里的类无效

export default Property; //应该是这个

除非您在同一行中导出和声明(这不是您显示的内容)


查看完整回答
反对 回复 2021-09-17
?
慕标琳琳

TA贡献1830条经验 获得超9个赞

我通过遵循@Valijon 建议并使用解决了这个问题

module.exports = Property;


查看完整回答
反对 回复 2021-09-17
  • 2 回答
  • 0 关注
  • 545 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信