首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:无法赋值给ReactJS中对象“”#<Object>“”的只读属性“exports”

问TypeError:无法赋值给ReactJS中对象“”#<Object>“”的只读属性“exports”
EN

Stack Overflow用户
提问于 2019-07-20 12:19:01
回答 1查看 266关注 0票数 0

所以我试图实现的是从我的应用程序中的页面中提取一个变量,并能够在其他页面中使用它。此变量是从导航栏操作的,该导航栏可从所需的每个页面访问。"App.js“基本上只是一个导航栏,其他页面显示在导航栏的下方。其他页面需要从导航栏中选择/分配的值,以便从数据库中提取信息。

App.js

代码语言:javascript
复制
let year;
let term;

class App extends Component {

  render() {
    // Here I do things and assign the variables on top values
  }

export default App;

module.exports = year;
module.exports = term;

然后,在我的一个页面上

代码语言:javascript
复制
var year = require("./AdminApp");
var term = require("./AdminApp");

class ViewStudents extends Component {
  constructor(props) {
    super(props);

    this.state = {
      year: year,
      term: term
    }
  }

  render() {
    // Do more magic
  }

export default ViewStudents;

这些是我的代码的汇总版本,其中包括我遇到问题的部分。如有任何帮助,我们不胜感激!提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-20 12:22:54

像这样使用命名导出。

App.js

代码语言:javascript
复制
let year;
let term;

class App extends Component {

  render() {
    // Here I do things and assign the variables on top values
  }

export default App;
export { year, term };

在您的页面中:

代码语言:javascript
复制
import { year, term } from "./AdminApp";

class ViewStudents extends Component {
  constructor(props) {
    super(props);

    this.state = {
      year: year,
      term: term
    }
  }

  render() {
    // Do more magic
  }

export default ViewStudents;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57121773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档