javascript – meteor js iron router:每当路由改变时应用CSS更改

我的应用程序中有主页,联系页面和其他几个与产品相关的页面.

目标是将背景图像应用于ONLY特定路线:/ homepage和/ contact.如果用户离开任一路线,请应用一些css更改.

我现在正在我的主页上用帮助器一起攻击这个,如下所示:

Template.homepage.rendered = function () {

    var route = Router.current();

    if ( route.path == '/' ) {

        document.body.className = "showBackgroundImage";

    }
};

部分胜利在这里,因为这将激活css,但我需要在路线改变时停用.我在router.js中也尝试了以下内容

this.route('homepage',{
    path: '/',onAfteraction: function  (argument) {
       // add a class name to body
       document.body.className = "showBackgroundImage";
    }
  });

和CSS在后台标准:

.showBackgroundImage { 
  background: url(bgImage.jpg) no-repeat center center fixed; 
}
最佳答案
这很容易使用iron:路由器布局并通过路由为每个页面应用不同的类.

首先,您需要定义主布局,例如: