[英]Override computed property in Ember 4 adapter
In the "new" class style of Ember / JavaScript (at least new to me), I am having trouble adapting old tutorials to set a computed property in my JSONAPIAdapter
.在 Ember / JavaScript 的“新”类样式中(至少对我来说是新的),我无法适应旧教程以在我的
JSONAPIAdapter
中设置计算属性。
export default class ApplicationAdapter extends JSONAPIAdapter {
@service session;
headers: computed('session.isAuthenticated', 'session.data.authenticated.token', function() {
if (this.session.isAuthenticated) {
return {
Authorization: `Bearer ${ this.session.data.authenticated.token }`,
Results in结果是
$TMPDIR/embroider/b3d2a6/adapters/application.js/application.js: Unexpected token (8:9)
6 | @service session;
7 |
> 8 | headers: computed('session.isAuthenticated', 'session.data.authenticated.token', function() {
The docs ( https://api.emberjs.com/ember-data/4.3/classes/JSONAPIAdapter/properties/headers?anchor=headers ) imply that this should work, but I think the @service session
line is messing things up.文档( https://api.emberjs.com/ember-data/4.3/classes/JSONAPIAdapter/properties/headers?anchor=headers )暗示这应该有效,但我认为
@service session
行把事情搞砸了。
What is the correct way to set the headers
property an Ember 4 class in 2022?在 2022 年将
headers
属性设置为 Ember 4 类的正确方法是什么?
Not sure exactly what error you're seeing, but you should be able to replace this with a getter不确定您到底看到了什么错误,但您应该可以用getter替换它
get headers() {
if (this.session.isAuthenticated) {
return {
Authorization: `Bearer ${ this.session.data.authenticated.token }
}
}
}
You can see a working example in our addapter您可以在我们的 addapter中看到一个工作示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.