开发者问题收集

类型错误:无法读取未定义的属性“code”?

2020-09-16
910

Dashboard.js

import React from 'react';

const Dashboard = () => {
    return (
        <!--  Card Section  -->
        <div className="row">
            <div className="col s12 m6 l3">
                <div className="card blue-grey darken-1">
                    <div className="card-content white-text">
                        <span className="card-title center">20</span>
                        <p className="center">Task of Today</p>
                    </div>
                </div>
            </div>

            <div className="col s12 m6 l3">
                <div className="card blue-grey darken-1">
                    <div className="card-content white-text">
                        <span className="card-title center">20</span>
                        <p className="center">Bugs</p>
                    </div>
                </div>
            </div>

            <div className="col s12 m6 l3">
                <div className="card blue-grey darken-1">
                    <div className="card-content white-text">
                        <span className="card-title center">20</span>
                        <p className="center">Finished Task</p>
                    </div>
                </div>
            </div>

            <div className="col s12 m6 l3">
                <div className="card blue-grey darken-1">
                    <div className="card-content white-text">
                        <span className="card-title center">20</span>
                        <p className="center">Remaining Task</p>
                    </div>
                </div>
            </div>
        </div>
    )
}

export default Dashboard;

从 app.js 导入后出现此错误

./src/components/Dashboard/Dashboard.js
TypeError: Cannot read property 'code' of undefined
    at parser.next (<anonymous>)
    at normalizeFile.next (<anonymous>)
    at run.next (<anonymous>)
    at transform.next (<anonymous>)

我不知道发生了什么。没有变量代码。它来自哪里? 如果我不导入 Dashboard,应用程序运行正常。当我在 app.js 中导入 Dashboard.js 时发生错误。

1个回答

尝试删除组件中的注释 <!-- Card Section --> ,它应该可以正常工作。

更正:

import React from 'react';

const Dashboard = () => {
    return (
        <div className="row">
            <div className="col s12 m6 l3">
                <div className="card blue-grey darken-1">
                    <div className="card-content white-text">
                        <span className="card-title center">20</span>
                        <p className="center">Task of Today</p>
                    </div>
                </div>
            </div>

            <div className="col s12 m6 l3">
                <div className="card blue-grey darken-1">
                    <div className="card-content white-text">
                        <span className="card-title center">20</span>
                        <p className="center">Bugs</p>
                    </div>
                </div>
            </div>

            <div className="col s12 m6 l3">
                <div className="card blue-grey darken-1">
                    <div className="card-content white-text">
                        <span className="card-title center">20</span>
                        <p className="center">Finished Task</p>
                    </div>
                </div>
            </div>

            <div className="col s12 m6 l3">
                <div className="card blue-grey darken-1">
                    <div className="card-content white-text">
                        <span className="card-title center">20</span>
                        <p className="center">Remaining Task</p>
                    </div>
                </div>
            </div>
        </div>
    )
}

export default Dashboard;
Quang Thái
2020-09-16