“导出声明只能出现在模块的顶层”

时间:2019-05-11 14:39:33

标签: javascript html es6-modules

我正在从一本书中学习JavaScript,在这里我试图从.js文件中导出一个类,以将其导入到html中。 我从Firefox收到以下错误:

  

导出声明只能出现在模块的顶层

我不明白,因为如果我将声明移到最上方,则什么都不会出现,并且错误仍然存​​在...
这是我的 index.html

<html>
<head>
    <meta charset="UTF-8">
</head>


<script src="./personne.js"></script>
<script src="./homme.js"></script>


<script type="module">
    import {Personne} from './personne.js';
    var personne = new Personne("NOM", "PRENOM", 1);
    personne.log();
</script>


<body>
    <center><h1>PAGE DE TEST</h1><center>
</body>
</html>

personne.js 源代码:

export { Personne };

class Personne{
    constructor(nom, prenom, numero){
        this.nom = nom;
        this.prenom = prenom;
        this.numero = numero;
    }
    log(){
        console.log("Personne " + this.numero + "\nNom : " + this.nom + "\n" + "Prenom : " + this.prenom);
    }
}

编辑:我必须将type =“ module”添加到<script src="./personne.js"></script>

0 个答案:

没有答案
相关问题