我开始在我的 Meteor 应用程序上使用 Coffeescript,虽然在包中一切正常,但我在将我的 js 文件模板函数转换为 .coffee 时遇到了一些问题。
尽管 coffeescript 在我编译时似乎是正确的,但在我呈现网页时我的助手和事件似乎并没有被触发。我什至在 Web 控制台上都没有收到错误消息。
我进行了搜索,但除了 this thread 之外什么也找不到。 .
我使用 Jade、Coffeescript,我的 .coffee 和 .jade 文件位于同一个文件夹中,如下所示:
client/templates/myTemplate/myTemplate.jade
client/templates/myTemplate/myTemplate.coffee
我尝试了 _ 方法,将我的文件重命名为 .html.jade 和 js.coffee,但到目前为止没有任何效果。如果我将计算出的 javascript 放在 myTemplate.js 文件中,一切正常。
有什么想法吗?
下面是代码示例:
Template.loginButton.helpers
statusText: () ->
console.log 'anybody there?'
if Meteor.user() then "Déconnexion" else "Connexion"
最佳答案
Coffeescript 对缩进很敏感。本站js2.coffee如果您不熟悉使用 coffeescript,这将非常有帮助。将你的代码粘贴到那里,输出以下 JS,表明你已经有效地生成了一个空操作:
Template.loginButton.helpers;
({
statusText: function() {
console.log('anybody there?');
if (Meteor.user()) {
return "Déconnexion";
} else {
return "Connexion";
}
}
});
另一方面,使用正确的缩进
Template.loginButton.helpers
statusText: () ->
console.log 'anybody there?'
if Meteor.user() then "Déconnexion" else "Connexion"
你得到
Template.loginButton.helpers({
statusText: function() {
console.log('anybody there?');
if (Meteor.user()) {
return "Déconnexion";
} else {
return "Connexion";
}
}
});
更新:由于这不是问题,请注意,如果您同时使用第三方模板系统 (jade) 和第三方 JS 转译器 (coffeescript),您您需要在 JS 之前先加载模板,以确保您可以从代码中访问模板。因此,无论是在您的 .meteor/packages
(对于应用程序)还是在 package.js
(对于包)中,确保 jade 出现在 coffeescript 之前。
附言我一直是咖啡用户,但我正在切换到 ES6,因为它具有咖啡的大部分重要功能,而且没有太多机会搬起石头砸自己的脚。此外,很难与不懂咖啡的人一起工作。
关于javascript - Meteor - Coffeescript 助手在与 Jade 位于同一文件夹中时未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31591759/