Node.js - 如何在 vash 模板中包含一个文件?

Node.js - How to include a file in a vash template?

我有一个使用 Express 和 Vash 的 Node.js 应用程序。我的布局如下所示:

layout.vash

<!DOCTYPE html>
<html>
@{
  model.items = MyClass.LoadItems();
}
  <head>
  </head>
  <body>
  </body>
</html>

MyClass 是一个 ES6 class 定义如下:

MyClass.js

'use strict';

class MyClass {
  constructor() {
  }

  static LoadItems() {
    var items = [];
    // ...
    return items;
  }
}

我的挑战是,当我包含 model.items = MyClass.LoadItems(); 行时,我的视图将不会加载。如果我注释掉该行,视图将按预期加载。我怀疑因为该行使用 ES6,所以存在一些编译问题。当我包含该行时,我在控制台中看到以下错误 window:

ERROR:
ReferenceError: Problem while rendering template at line 2, character 16.
Original message: Problem while rendering template at line 2, character 16.
Original message: MyClass is not defined.
Context: 

     1 | <!DOCTYPE html>
  >  2 | <html lang="en">
     3 | @{   
     4 |     model.items = MyClass.LoadItems();
     5 |     var navItemsB = [];

.
Context: 

     1 | @html.extend('layout', function(model){
  >  2 |     @html.block('content', function(model){            
     3 |       <div>
     4 |         <div 

有没有办法让 MyClasslayout.vash 内可访问?如果是,怎么做?

谢谢!

一般来说,在 express 中(就像在大多数视图引擎中一样),当您调用渲染函数时,您会传递模型中需要的任何属性。您可以在模型对象中传递任何您想要的内容,包括 class (最后是一个函数 - 因此是对象):

app.get('/', function (req, res) {
  res.render('layout', { MyClass });
});