interface State {
price: number;
count: number;
}
export default class ClassT extends BaseComponent<{}, State> {
constructor(props) {
super(props);
this.state = {
price: 100,
count: 1
}
this.onIncrease = this.onIncrease.bind(this);
this.onDecrease = this.onDecrease.bind(this);
}
componentDidMount() {
ClassT.printSomething();
ClassT.asyncPrintSomething();
this.doSomethings();
this.asyncDoSomethings();
}
@catchMethod({ message: "printSomething error", toast: true })
static printSomething() {
throw new CatchError("printSomething error: 主动抛出");
console.log("printSomething:", Date.now());
}
@catchMethod({ message: "asyncPrintSomething error", toast: true })
static async asyncPrintSomething() {
const { run } = delay(1000);
await run();
throw new CatchError("asyncPrintSomething error: 主动抛出");
console.log("asyncPrintSomething:", Date.now());
}
@catchGetter({ message: "计算价格失败", toast: true })
get totalPrice() {
const { price, count } = this.state;
// throw new Error("A");
return price * count;
}
@catchMethod("增加数量失败")
async onIncrease() {
const { run } = delay(1000);
await run();
this.setState({
count: this.state.count + 1
})
}
@catchMethod("减少数量失败")
onDecrease() {
this.setState({
count: this.state.count - 1
})
}
@catchInitializer({ message: "catchInitializer error", toast: true })
doSomethings = () => {
console.log("do some things");
throw new CatchError("catchInitializer error: 主动抛出");
}
@catchInitializer({ message: "catchInitializer async error", toast: true })
asyncDoSomethings = async () => {
const { run } = delay(1000);
await run();
throw new CatchError("catchInitializer async error: 主动抛出");
}
render() {
const { onIncrease, onDecrease } = this;
const totalPrice = this.totalPrice;
return padding: "150px",
lineHeight: "30px",
fontSize: "20px"
}}>
价格:{
this.state.price}<
/div>
数量:1div>