Meteor - 在每次页面加载时执行代码

Meteor - Execute code on every page load

我想在任何页面加载时执行一个函数。

Meteor.startup()Template.myTemplate.onRendered() 之类的东西不是我想要的,因为它们只在应用程序加载时触发一次。

基本上我需要一个每次 URL 变化时触发的事件,有吗?

您可以在每次路由更改时使用 onRunonBeforeAction 至 运行 任意代码。

Router.onRun(function(){
  console.log('onRun', this.current().route.getName());
  this.next();
});

Router.onBeforeAction(function(){
  console.log('onBeforeAction', this.current().route.getName());
  this.next();
});

使用此占位符代码检测此代码何时实际 运行。

onRun 将 运行 每次路线更改仅一次。 (适合分析相关的东西) onBeforeAction 将在当前路由数据上下文被修改时反应性地重新运行。