[英]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.