Express JS从数据库加载全局变量

时间:2019-03-02 13:52:28

标签: javascript node.js express

我正在构建全局函数,它是app.js中这样的中间件:

// ---- HomeComponent ----

ngOnInit() {
  // To navigate to a route once the activeBusinessCase has a non null value
  // subscribe but only take the first non null element and no further values afterwards
  this.authService.activeBusinessCase$
    .pipe(first(Boolean))
    .subscribe(bc => this.router.navigate([Constants.routing.explorer + bc.toLowerCase()]));
}

// ---- SmagHttpService & UserAuthService ----

// Don't subscribe to activeBusinessCase$ in here!! Instead use
this.authService.activeBusinessCase$.getValue()
// when you need the current value (which might not have been set yet, 
// but you already have null checks here and if this part of your app worked before 
// there will be no differences)

// Example:
const businessCase = this.authService.activeBusinessCase$.getValue();
if (businessCase) {
  this.router.navigate([Constants.routing.explorer + businessCase.toLowerCase()]);
}

businessCase ? businessCase : environment.default_business_case

我的问题是globals.js对API执行了HTTP请求。加载用户特殊数据,例如导航。因为它是中间件,所以请求可以多次执行。是否有其他解决方案可以做到这一点,而无需请求每个路由器。我的一些路由器会生成前端javascript文件,这就是为什么多次执行请求的原因。

0 个答案:

没有答案