【发布时间】:2015-09-21 18:52:27
【问题描述】:
我有一个使用 Express 和 Vash 的 Node.js 应用程序。我的布局如下所示:
layout.vash
<!DOCTYPE html>
<html>
@{
model.items = MyClass.LoadItems();
}
<head>
</head>
<body>
</body>
</html>
MyClass 是这样定义的 ES6 类:
MyClass.js
'use strict';
class MyClass {
constructor() {
}
static LoadItems() {
var items = [];
// ...
return items;
}
}
我的挑战是,当我包含 model.items = MyClass.LoadItems(); 行时,我的视图将不会加载。如果我注释掉该行,视图将按预期加载。我怀疑因为该行使用 ES6,所以存在一些编译问题。当我包含该行时,我在控制台窗口中看到以下错误:
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
有没有办法让MyClass 在layout.vash 中访问?如果有,怎么做?
谢谢!
【问题讨论】:
-
我注意到的第一件事是它应该是
new MyClass().LoadItems();,但你仍然缺少一些东西。我不熟悉 vash,但看起来您需要以某种方式包含MyClass.js。 -
@AnotherDev。我同意我需要包含
MyClass。我不确定如何。就像我不能在 Vash 中使用require一样。这似乎很奇怪。我不需要new,因为该函数是静态可见的。 -
明白了,你是怎么用 es5 做的?
标签: javascript node.js vash