【发布时间】:2021-02-14 16:23:07
【问题描述】:
我有一个包含静态 URL 的抽象类,并使用该静态 URL 实现功能。子级应覆盖 A 类的静态 URL。
但我仍然希望孩子们使用其父类 A 提供的功能来使用子类的静态覆盖 URL。有什么办法可以在 Typescript 中做到这一点?
interface IState {
cashflowToPost?: CashflowObject;
cashflowList: CashflowObject[];
}
export default abstract class CashflowListView extends React.Component<{}, IState> {
static cashflowObject : CashflowObject;
static getURL : URL;
callApi() {
try {
axios.get(CashflowListView.getURL.toString()).then(response => {
console.log("called Api");
this.setState({cashflowList: response.data});
})
} catch {
console.log("failed to call api!")
}
}
componentDidMount() {
this.callApi();
}
}
【问题讨论】:
-
考虑到每个类都应该附加一个 URL,所以基于类而不是基于实例,我认为静态是正确的方法吗?我只是希望孩子们在 axios 调用中使用他们自己的静态 url 而不是 CashflowListView.getURL.toString()
-
你不能那样做。父级的方法将始终引用父级的静态变量。
标签: javascript typescript inheritance static