繁体   English   中英

module.export不导出文件,要导入到main.js中

[英]module.export not exporting the file, to be imported in main.js

我是Java的新手,并且使用Visual代码作为IDE来处理Java。

我正在阅读一些教程,但遇到了无法解决的问题

我正在使用module.export导出我的js文件,并且正在使用require在我的主文件中使用它。 但是,它不起作用,因为我希望弹出警报消息。

以下是我的代码

    **task.js**
    /* jshint node: true */
    'use strict';
    class Task {

            constructor(name) {
                this.name = name;
            }

            completed() {
                alert(this.name);
            }
     }
     module.export = Task;

     **cat.js**
    /* jshint node: true */
    'use strict';
    function Cat(name) {
        this.name = name;

        this.toString = function() {
                alert(name);
        };
    }

    Cat.prototype.complete = function() {
        console.log('Completed');
    };
    module.export = Cat;

    **main.js**
    var Task = require('./task');
    var Cat = require('./cat');
    // Ways to create instances.

    var task1 = new Task("A");

    task1.completed();


    var cat1 = new Cat("Fluffy");
    cat1.toString();
    cat1.complete();

  **Demo1.html**
    <!DOCTYPE html>
    <html>
        <head>

        </head>
        <body>
        <h1> Hello! </h1>
        <p id="demo"></p>

        <script type="text/javascript" src="script/main.js"></script>
        </body>
    </html> 

目录结构:

.vscode
   script
     cat.js
     task.js
     main.js
demo1.html

我在输出控制台上没有收到错误。 任何帮助,将不胜感激。

看起来您只是有错字! 使用module.exports而不是module.export

Node.js文档

如果您对javascript的未来感兴趣,可以看一下具有稍微不同语法的exportExport Doc )。 大多数浏览器尚不支持它,因此需要使用编译器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM