Javascript Node.js-如何在vash模板中包含文件?
我有一个Node.js应用程序,它使用Express和Vash。我的布局如下所示: layout.vash 我的挑战是,当我包含model.items=MyClass.LoadItems时,我的视图将不会加载;线如果注释掉该行,视图将按预期加载。我怀疑由于该行使用ES6,因此存在编译问题。当我包含该行时,我在控制台窗口中看到以下错误:Javascript Node.js-如何在vash模板中包含文件?,javascript,node.js,vash,Javascript,Node.js,Vash,我有一个Node.js应用程序,它使用Express和Vash。我的布局如下所示: layout.vash 我的挑战是,当我包含model.items=MyClass.LoadItems时,我的视图将不会加载;线如果注释掉该行,视图将按预期加载。我怀疑由于该行使用ES6,因此存在编译问题。当我包含该行时,我在控制台窗口中看到以下错误: ERROR: ReferenceError: Problem while rendering template at line 2, character 16.
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
有没有办法让我的类可以在layout.vash中访问?如果是,怎么做
谢谢 一般来说,在express中,就像在大多数视图引擎中一样,在调用渲染函数时,可以传递模型中所需的任何属性。您可以在模型对象中传递您想要的任何内容,包括一个最终是函数的类-因此对象:
app.get('/', function (req, res) {
res.render('layout', { MyClass });
});
我注意到的第一件事是它应该是新的MyClass.LoadItems;,但你还是错过了一些东西。我不熟悉vash,但看起来您需要以某种方式包含MyClass.js。@AnotherDev。我同意我需要加入我的班级。但我不知道怎么做。好像我不能在Vash中使用require。这似乎很奇怪。我不需要新的,因为函数是静态可见的?
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
app.get('/', function (req, res) {
res.render('layout', { MyClass });
});