React와 Electron으로 프로젝트를 진행 중이며 오류가 있습니다. 두 개의 변수 형태로 제공되는 props를받는 생성자가있는 구성 요소가 있습니다. 생성자는 별도의 파일에서 인스턴스화됩니다. 문제는 변수 console.log
가 생성자에서 잘 작동하지만 (예를 들어 출력하려면) 변수가 정의되지 않은 상태로 돌아온다는 것입니다.
나는 이미 .bind
그것을 묶는 데 사용하려고 시도했지만 도움이되지 않았으며 여전히 정의되지 않은 것으로 나타났습니다.
생성자가 호출되는 곳입니다.
const dropDown = new Dropdown({
editor,
monaco
});
다음은 변수를 사용하려는 생성자와 예입니다.
constructor(props) {
super(props);
// Define variables
this.editor = props.editor;
this.monaco = props.monaco;
// Returns correct object
console.log(this.monaco);
// Bind functions
this.changeLanguage = this.changeLanguage.bind(this);
this.filterFunction = this.filterFunction.bind(this);
this.dropDown = this.dropDown.bind(this);
}
changeLanguage(language) {
// Returns undefined all the time
console.log(this.monaco);
this.monaco.editor.setModelLanguage(this.editor, language);
}
변수가 생성자와 파일의 다른 위치에서 동일 할 것으로 예상하지만 어떤 이유로 생성자에서만 정의됩니다.
this.props.monaco를 사용할 수 있습니다. 또는 다시 작성하면 props를 changeLanguage 함수에 전달할 수 있습니다.
changeLanguage(language, props) {
// Returns undefined all the time
console.log(props.monaco);
props.monaco.editor.setModelLanguage(this.editor, language);
}
그것을 호출하십시오 : this.changeLanguage('English', this.props)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다