我試圖動態地導入/需要組件,但不知何故,當我做它React抱怨。 require函數找到它,但React拋出一個錯誤,說它缺少一些函數't'等。所有這些都在電子應用程序中。導入或要求React組件動態
我有一個嚮導設置(這是工作,但不是很優雅,我認爲),其中每個頁面都有它自己的佈局和jsx組件。如果我想添加一個新頁面,我不想管理x個文件,而目前我必須由於我目前的設置。 下面你可以找到我想要達到的目標以及我現在在做什麼來達到目標。如果有任何建議,代碼味道或更好的選擇,請讓我知道,因爲我對React和ES2015相當陌生(因爲我來自C#背景)。
我試圖實現
export default class WizardPageContainer extends Component {
// Constructor
constructor(props) {
super(props);
}
// Render
render() {
const WizardPage = require(`./path/to/${this.props.step.id}`);
return (
<WizardPage step={this.props.step} />
);
}
}
如何我目前做:這意味着我不得不進口/文件先申報的「WizardPageContainer」組件之上。這意味着額外的工作,容易出錯/遺忘。 我要補充,該代碼工作,現在好了,但我不認爲這是優雅的/面向未來:
/* PAGES */
import WizardPage_Welcome from './pages/0.wizard.welcome';
import WizardPage_SystemCheck from './pages/1.wizard.systemCheck';
import WizardPage_SignIn from './pages/2.wizard.signIn';
import WizardPage_ExamCode from './pages/3.wizard.examCode';
import WizardPage_TakeExamination from './pages/4.wizard.takeExamination';
import WizardPage_Close from './pages/5.wizard.close';
const pages = [
WizardPage_Welcome,
WizardPage_SystemCheck,
WizardPage_SignIn,
WizardPage_ExamCode,
WizardPage_TakeExamination,
WizardPage_Close
];
/*/********************************************************************///
/* ******************************************************************** */
/* COMPONENT */
export default class WizardPageContainer extends Component {
// Constructor
constructor(props) {
super(props);
}
// Render
render() {
const WizardPage = pages[`${this.props.step.id}`];
return (
<WizardPage step={this.props.step} />
);
}
}
/*/********************************************************************///
可能在http://stackoverflow.com/a/36678030/1155847找到了解決方案。我會試試這個報告,或者報告問題,如果這個問題有效的話。 –