简体   繁体   English

为什么在Angular 2+中更喜欢将共享变量放在服务中,而不是将它们存储在可以直接导入的常量对象中?

[英]Why in Angular 2+ is it preferred to put shared variables in services rather than store them in a constant object that can be imported directly?

I am creating a SPA in Angular 7, and want to follow best practices, but I don't fully understand why everywhere recommends storing data in services rather than in a file with a constant that can be directly imported. 我正在Angular 7中创建SPA,并且希望遵循最佳实践,但是我不完全理解为什么到处建议将数据存储在服务中,而不是建议将数据存储在具有可直接导入的常量的文件中。

Directly importing the constant seems a lot simpler. 直接导入常量似乎要简单得多。 Am I missing something important? 我缺少重要的东西吗?

My code: 我的代码:

userSession.ts userSession.ts

export const userSession = {
    loggedIn: null,
    userId: null
};

something.service.ts : something.service.ts

import { userSession } from '../appGlobals/user-session';
// decorators, ect .......
export class SomethingService {  
  constructor() { }
  doSomething() {
    if (userSession.loggedIn) {
      // do something
    }
  }
}

Suggested code (to my understanding): 建议的代码(据我了解):

user.service.ts user.service.ts

//imports and decorator ....
export class UserService {
  session = {
    loggedIn: boolean;
    userId: number;
  }
//...
}

some.service.ts some.service.ts

import { UserSession } from '../services/user.service.ts';
// decorators, ect .......
export class SomeService {
  constructor(private userService: UserService) { }
  doSomething() {
    if (this.userService.session.loggedIn) {
      // do something
    }
  }
}

With large classes that use the user session fields, the code seems to be a lot cleaner if to just use "userSession" object instead of "this.userService.session" and inject the dependency into the constructor. 对于使用用户会话字段的大型类,如果仅使用“ userSession”对象而不是“ this.userService.session”并将依赖项注入到构造函数中,则代码看起来更简洁。 So what are the advantages/disadvantages of each setup? 那么每种设置的优点/缺点是什么?

In my opinion there is no difference in the example you are describing, however most of the times these shared variables might not be prepared when a component using them is completely loaded, ie the variable might be filled after an API call. 在我看来,您所描述的示例没有什么区别,但是在大多数情况下,当使用它们的组件完全加载时,可能无法准备这些共享变量,即,在API调用之后可能会填充该变量。 For that reason services are preferred that can incorporate proper functionalities to manipulate these shared variables in a proper and clean way. 因此,首选的服务应具有适当的功能,以适当且干净的方式操纵这些共享变量。 Also keep in mind that most of these shared variables are some sorts of Observable objects that can notify the components using them when the value is ready (ie applies to scenario when values are filled afterwards or are subject to changes). 还请记住,这些共享变量中的大多数都是某种Observable对象,它们可以在值准备好时通知使用它们的组件(即适用于事后填充值或会发生更改的情况)。

One big advantage is testing. 测试的一大优点是。

In your code, userSession is declared outside of the class therefore it would be hard to write unit test code. 在您的代码中, userSession在类之外声明,因此很难编写单元测试代码。

But in the suggested one, the service is injected in constructor so test code can be written with the service or even mock service. 但是在建议的一种中,该服务被注入到构造函数中,因此可以使用该服务甚至是模拟服务编写测试代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 2+-如何模拟用于环境变量的常量类 - Angular 2+ - How to mockup constant class used for environment variables Angular 2+对angularJs的服务 - Angular 2+ services to angularJs 如何使导入组件函数内的变量反映到它们的当前值而不是它们在angular8中的初始值? - How to make the variables inside an imported component's function to reflect to their present values rather than their initial values in angular8? 当组件之间的共享对象更改时,Angular 2+ Catch 事件 - Angular 2+ Catch event when shared object between components changes Angular 2+中动画的变量 - Variables on Animations in Angular 2+ 如何在 AppComponent 以外的其他组件中使用从共享模块导入的 Angular 组件 - How can I use angular component imported from a shared module in another component other than AppComponent Angular 2+和ngBootstrap-共享模式 - Angular 2+ & ngBootstrap - Shared Modals 令人困惑的Angular 2+模块,导出和服务-如何避免共享/可重用服务的多个实例 - Confusing Angular 2+ modules, exports and services - how to avoid multiple instances of shared/reusable service Angular 2 DI注入功能而非对象 - Angular 2 DI Injects function rather than object 为什么angular 2+模块或组件应该每次都需要而不是一次导入? - Why angular 2+ modules or components should be imported each time is required instead of once?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM