无法将 JavaScript 变量插入 Jade

Cannot insert JavaScript variable into Jade

我正在尝试将一个普通的 JavaScript 表达式插入到 Jade 文件中。但是当我将 isAccepted 变量添加到我的 code.This 是错误消息时,我的应用程序不断收到错误消息。

Your app is crashing. Here's the latest log.

Errors prevented startup:

While building the application: jade-test.jade: Jade syntax error: Expected identifier, number, string, >boolean, or null {{isAccepted ? 'class1' : 'class2... ^ packages/compileJade/plugin/handler.js:44:1: Cannot read property 'head' >of undefined (compiling jade-test.jade) (at fileModeHandler)

Your application has errors. Waiting for file change.

这是我的代码:

玉-test.jade

template(name="layout")
  h1 This is layout template
  h2 This is h2 paragraph
  h3 This is h3 paragraph
   +hello

template(name="hello")
  h4 This is home template
  button Click me
  p You have click me #{counter} times.
  - var isAccepted = true
  button(class=isAccepted ? 'class1' : 'class2') I'm the right choice.

玉-test.js

if (Meteor.isClient) {
  // counter starts at 0
  Session.setDefault('counter', 0);

  Template.hello.helpers({
    counter: function () {
      return Session.get('counter');
    }
  });

  Template.hello.events({
    'click button': function () {
      // increment the counter when button is clicked
      Session.set('counter', Session.get('counter') + 1);
    }
  });

  Router.route('/', function() {
    this.render('layout');
  });
 }

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}

不确定是否支持在 jade 中插入这样的 javascript 代码,您可能应该改用助手。

我认为三元表达式语法也无效,再次使用帮助程序。

翡翠

template(name="hello")
  h4 This is home template
  button Click me
  p You have click me #{counter} times.
  button(class=isAccepted) I'm the right choice.

JS

Template.hello.helpers({
  isAccepted: function(){
    var isAccepted = true;
    return isAccepted ? "class1" : "class2";
  }
});