我正在尝试在对象中添加属性,如下所示:
request(options:HttpRequestData): Promise<any> {
options.headers = options.headers || {};
options.headers['Accept'] = 'application/json';
options.headers['Content-Type'] = 'application/json'; // Error
return this._request(options);
}
但它有错误:
options.headers['Content-Type'] = 'application/json';
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
assignment of computed property/element. Computed property/element cannot be assigned on possibly undefined value
HttpRequestData是一个对象类型:
type HttpRequestData = {
url:string,
method:string,
headers?:Object,
body?:string,
};
仅将一个值分配给options.header(在本例中为“Accept”)可以正常工作,但当我尝试两个或更多时,它会失败。
Flow中单个分配和多个分配之间有什么区别,以及如何将这些属性分配到options.headers对象中?
任何建议都会非常感激。
答案 0 :(得分:0)
我解决了这个问题:
override func viewDidLoad() {
super.viewDidLoad()
// main tab bar controller
self.initTabBarMainController()
// login page
self.initLoginViewController()
// checking the session
let tokenExpired = SessionManager.sharedInstance.isTokenExpired()
var presentedViewController: UIViewController? = nil
if Utilities.isLoggedIn() && SessionManager.sharedInstance.getSessionToken() != nil && !tokenExpired
{
presentedViewController = self.mainTabViewController!
} else {
presentedViewController = self.loginViewController!
}
self.addChildViewController(presentedViewController!)
presentedViewController!.view.frame = self.view.frame
self.view.addSubview(presentedViewController!.view)
self.setupNotifications()
}
现在没有更多错误消息。