import UserService from './UserService';export default class Controller { constructor() { this.userService = new UserService(); //初始化service } login(req, res, next) { var user = {'account': 'zhangsan', 'pwd': '123'}; this.userService .login(user) .then((result)=>{ // console.log(result); res.json(user); }) .catch((error)=>{ console.log(error); }); }}報錯如下而如果我把new UserService()去掉,改為在login里 new UserService().login(user)就沒有問題,但是卻似乎違反了面向對象的原則。 我目的是想實現初始化的時候只有一個控制器和一個UserService,而不是每次登陸都要new一個UserService出來。請求高玩指點迷津!附:給予看到此貼的朋友結果為this指向問題,可參考https://segmentfault.com/q/10...
es6關于在constructor中new對象為屬性的問題,報錯
阿晨1998
2018-10-16 13:15:18