【发布时间】:2017-04-19 14:36:57
【问题描述】:
我正在尝试动态导入/需要组件,但是当我这样做时,React 会抱怨。 require 函数确实找到了它,但是 React 抛出一个错误,说它缺少一些函数 't' 等。所有这些都在一个电子应用程序中。
我有一个向导设置(可以工作,但我认为不是那么优雅),每个页面都有自己的布局和 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} />
);
}
}
/*/********************************************************************///
【问题讨论】:
-
可能已经在stackoverflow.com/a/36678030/1155847 找到了解决方案。如果可行,我会尝试并报告或关闭问题。
标签: javascript node.js reactjs electron