在我的Angular2项目中,我收到此错误:
"声明实例方法后不允许声明实例字段。相反,这应该在类/接口的开头。 (构件排序)"
我想了解如何解决这个问题,以及为什么我会这样做。
错误与下一个代码中的私有函数有关:
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;
}
});
}
}
答案 0 :(得分:9)
我猜你的项目有一些类型的linting设置,可以在构建时检查样式问题。
要修复它,你只需按照它说的那样做。在任何方法调用之前将代码移到。
export class HomeComponent implements OnInit {
public error: string;
public shirts = [];
private sizeScore = {
'string': -1,
s: 0,
m: 1,
l: 2,
'x-large': 3,
};
// ...
答案 1 :(得分:3)
它实际上不是编译/运行时错误,而是代码linting问题。
将您班级的所有属性置于方法之上是一种很好的做法,因此如果您只是将package com.medum.medum.adapter;
import android.app.Activity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.medum.medum.R;
import com.medum.medum.model.Picture;
import java.util.ArrayList;
public class CardAdapterRecyclerView extends RecyclerView.Adapter<CardAdapterRecyclerView.CardViewHolder>{
private ArrayList<Picture> pictures;
private int resource;
private Activity activity;
public CardAdapterRecyclerView(ArrayList<Picture> pictures, int resource, Activity activity) {
this.pictures = pictures;
this.resource = resource;
this.activity = activity;
}
@Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(resource,parent,false);
return new CardViewHolder(view);
}
@Override
public void onBindViewHolder(CardViewHolder holder, int position) {
Picture picture = pictures.get(position);
holder.titlecard.setText(picture.getTitlecard());
holder.pricecard.setText(picture.getPricecard());
holder.descriptioncard.setText(picture.getDescriptioncard());
holder.timecard.setText(picture.getTimecard());
}
@Override
public int getItemCount() {
return pictures.size();
}
public class CardViewHolder extends RecyclerView.ViewHolder{
private CardView pictureCard;
private TextView titlecard;
private TextView pricecard;
private TextView descriptioncard;
private TextView timecard;
public CardViewHolder(View itemView) {
super(itemView);
pictureCard = (CardView) itemView.findViewById(R.id.picturecard);
titlecard = (TextView) itemView.findViewById(R.id.cardtitle);
pricecard = (TextView) itemView.findViewById(R.id.price);
descriptioncard = (TextView) itemView.findViewById(R.id.description);
timecard = (TextView) itemView.findViewById(R.id.timecard);
}
}
}
移到顶部,它就会停止说。
有关此规则的更多信息DataFrame.tail(n)
。
答案 2 :(得分:0)
根据here我认为你只需要将这个字段的声明移到这些方法的声明之上。
答案 3 :(得分:0)
Row
和public-before-private
现已弃用