[英]Getter Returns Undefined
我有一個靜態的吸氣劑,它可以在類中獲取變量。 但是,getter返回的是undefined-通過this._title
獲得變量this._title
效果很好。
import React, { Component } from 'react';
class ParentClass extends Component {
_title = "Parent Class";
static get title() {
return this._title;
}
}
export default class ChildClass extends ParentClass {
_title = "Child Class";
render() {
return (
<div>
<p>{"By Variable: " + this._title}</p>
<p>{"By Getter: " + this.title}</p>
</div>
)
}
}
這是渲染的結果: 結果
更新:
這是我的應用程序,或者試圖從外部訪問該類的內容:
import React, { Component } from 'react';
import ChildClass from './pages/Status';
class App extends Component {
render() {
return (
<div className="App">
<div className="App-header">
<img src={byu} className="App-logo" alt="logo" />
<h2>Welcome to the P&G System Status Utility</h2>
</div>
<p className="App-intro">
To get started, select a server:
</p>
<p>{"Outside Title: " + ChildClass.title}</p>
<p>{"Outside Description: " + ChildClass.description}</p>
<p>{"Outside Author: " + ChildClass.author}</p>
<ChildClass/>
<Scanner/>
</div>
);
}
}
export default App;
這是ChildClass(及其擴展的ParentClass)
import React, { Component } from 'react';
class ParentClass extends Component {
_title = "Debug";
_description = "Debug Description";
_author = "Arbiter";
get title() {
return this._title;
}
set title(value) {
this._title = value;
}
get description() {
return this._description;
}
set description(value) {
this._description = value;
}
get author() {
return this._author;
}
set author(value) {
this._author = value;
}
}
export default class ChildClass extends ParentClass {
title = "Child Class";
render() {
return (
<div>
<p>{"Title Inside: " + this.title}</p>
<p>{"Description Inside: " + this.description}</p>
<p>{"Author Inside: " + this.author}</p>
</div>
)
}
}
結果就是: 在此處輸入圖片描述
(我完全不知道StackOverflow如何工作)
我有一個靜態吸氣劑……
這就是問題所在。 它不應該是static
。 您在聲明ParentClass.title
而不是實例屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.