ES 6 - 使用课程

时间:2016-04-04 14:08:21

标签: javascript node.js express

我正在学习ES 6的类语法。我来自C#背景,所以如果我的术语不正确,我会道歉。或者,如果我做的事看起来很奇怪。

我正在构建一个网络应用程序作为学习练习。它建立在Node和Express之上。我有一些像这样定义的路线:

'use strict';

module.exports = function() {
    const app = this;

    app.use('/blog', function(req, res) {
        console.log('loading blog postings');        
        res.render('blog', {}); 
    });

    app.use('/', function(req, res) {
        console.log('looking up: ' + req.path);
        res.render('home', {});
    });
};

我试图将一些viewModel放在这些视图后面。所以,我有一个名为viewModels的目录。该目录包含以下文件:

index.js
blog.js
home.js

目前的文件可能不准确,如下所示:

index.js

'use strict';

module.exports = function() {
  const HomeViewModel = require('./home);
  const BlogViewModel = require('./blog);
};

blog.js

export default class BlogViewModel {
    constructor() {
        this.title = 'My Blog';
    }
}

home.js

export default class HomeViewModel {
    constructor() {
        this.title = 'Home';
    }
}

我的想法是,我可以使用index.js作为定义我的包或命名空间的方法。然后,在我的路由代码中,我可以做这样的事情:

'use strict';

module.exports = function() {
    const app = this;
    const ViewModels = require('../viewModels/index');

    app.use('/blog', function(req, res) {
        console.log('loading blog postings');
        let viewModel = new ViewModels.BlogViewModel();
        res.render('blog', viewModel); 
    });

    app.use('/', function(req, res) {
        console.log('looking up: ' + req.path);
        let viewModel = new ViewModels.HomeViewModel();
        res.render('home', viewModel);
    });
};

然而,当我尝试这个时,我得到一些运行时错误,说"错误:找不到模块' ../ viewModels / index'"。这意味着我没有正确设置我的模块。但是,看起来我在做错了什么?

2 个答案:

答案 0 :(得分:3)

您的index.js文件不正确,您不从那里导出ViewModels。将其更改为:

'use strict';

module.exports = {
  HomeViewModel: require('./home'),
  BlogViewModel: require('./blog')
};

并且... viewModels对C#有好处,但不适用于Node.js.在节点中,它应该只是模型,IMO。

<强>更新

Node.js并不完全支持所有ES6功能,尤其是新模块声明:https://nodejs.org/en/docs/es6/。您应该使用标准的CommonJs模块声明来导出您的函数:

'use strict';

class HomeViewModel {
  constructor() {
    this.title = 'Home';
  }
}

module.exports = HomeViewModel;

答案 1 :(得分:0)

实际上,我不确定你想问什么。如果我回答错误的东西,请不要介意。

首先,您收到错误Error: Cannot find module '../viewModels/index'的原因是因为您在那里放了两个点。它应该只是一个点意味着从这里开始。但是,我不确定这是不是问题。我想问你放routing code的位置,但我还没有发表评论的权限。 (啊......你开玩笑的堆栈溢出......)

其次,这是在ES6中导出类的正确方法。

例如:

<强> AClass.js

'use strict';
//This module can be used within the class. However, you cannot use it in another file.
const AModule = require('AModule');
//The class name used here just for debug output.
module.exports = class AClass {
  constructor(startValue) {
    //Not like C#. JavaScript does not define private or public.
    this.value = startValue;
  }
  method(incValue) {
    this.value += incValue;
    AModule(); //Just show you can use this module within the class;
  }
}

<强> main.js

'use strict';
//You need to put a ./ before the path if you are include another module made by yourself.
//You do not need it for a npm module.
const AClass = require('./AClass.js');
//Now you just required the class of the AClass, you still need to new one;
var aClass = new AClass(500);
//After new one, you can call its method.
aClass.method(30);
//You can access its property by a dot;
console.info(aClass.value); //530

这是在ES6中创建课程的100%工作方式。

以下是详细文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

无论如何,JavaScripts中的类就像函数一样,函数prototype将是它的方法。 new Class()只是创建一个对象,运行函数Class,对象为this(与Class.bind(obj, parameters)相同),然后将新对象的构造函数属性链接到使用的函数。

module.exports = xxx只是让xxx成为这个模块的值。例如,如果您module.exports = 'Hello';console.info(require('module'));,您将获得Hello。