typescript 这个错误“声明实例方法后不允许声明实例字段”是什么意思。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45514843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the meaning of this error "Declaration of instance field not allowed after declaration of instance method."
提问by Jakub
In my Angular2 project I receiving this error:
在我的 Angular2 项目中,我收到此错误:
"Declaration of instance field not allowed after declaration of instance method. Instead, this should come at the beginning of the class/interface. (member-ordering)"
“在声明实例方法之后不允许声明实例字段。相反,这应该出现在类/接口的开头。(成员排序)”
I would like to understand how to resolve this and why I'm getting this.
我想了解如何解决这个问题以及为什么我会得到这个。
The error is related to the private function in the next code:
该错误与下一段代码中的私有函数有关:
export class HomeComponent implements OnInit {
public error: string;
public shirts = [];
constructor(public rest: RestService,
public scService: ShoppingCartService,
public snackBar: MdSnackBar) {
}
ngOnInit() {
this.rest.getAll().subscribe((r: any) => {
this.shirts = r;
}, error => {
this.error = 'Opps there\'s some error';
});
}
addToCart(shirt: any) {
this.scService.add(shirt);
this.showSnackMessage('Added to Chart List');
}
showSnackMessage(message: string) {
this.snackBar.open(message, null, {
duration: 1000
});
}
//Here the error is showed
private sizeScore = {
'string': -1,
s: 0,
m: 1,
l: 2,
'x-large': 3,
};
sortData(sort: Sort) {
const data = this.shirts.slice();
if (!sort.active || sort.direction === '') {
this.shirts = data;
return;
}
this.shirts = data.sort((a, b) => {
let isAsc = sort.direction === 'asc';
switch (sort.active) {
case 'colour':
return compare(a.colour, b.colour, isAsc);
case 'size':
return compare(this.sizeScore[a.size], this.sizeScore[b.size], isAsc);
default:
return 0;
}
});
}
}
回答by DeborahK
I would guess that your project has some type of linting set up that checks for style issues at build time.
我猜你的项目有某种类型的 linting 设置,可以在构建时检查样式问题。
To fix it you just need to do as it says. Move the code up to before any method calls.
要修复它,您只需要按照它说的去做。将代码向上移动到任何方法调用之前。
export class HomeComponent implements OnInit {
public error: string;
public shirts = [];
private sizeScore = {
'string': -1,
s: 0,
m: 1,
l: 2,
'x-large': 3,
};
// ...
回答by Edmundo Rodrigues
It's not actually a compilation/runtime error, but a code linting issue.
这实际上不是编译/运行时错误,而是代码 linting 问题。
It's a good practice to place all properties of your class above the methods, so if you just move private sizeScore
to the top, it will stop saying.
将类的所有属性放在方法上方是一个很好的做法,所以如果你只是移到private sizeScore
顶部,它就会停止说。
More information about this rule here.
有关此规则的更多信息,请点击此处。
回答by arthurakay
Per the TSLint docsI think you just need to move your declaration of the field above the declarations of those methods.
根据TSLint 文档,我认为您只需要将字段声明移动到这些方法的声明之上。
回答by Sandy
public-before-private
and static-before-instance
are now deprecated
public-before-private
而static-before-instance
现在已被弃用