在我的角度2应用程序中,我有一个使用库中的Observable类的服务rxjs.
import { Observable } from 'rxjs';
Run Code Online (Sandbox Code Playgroud)
目前我正在使用,Observable以便我可以使用该toPromise()功能.
我在另一个StackOverflow问题中读到了以这种方式导入并且还从rxjs/Rx导入中导入了大量不必要的东西,rxjs这将增加页面加载时间和/或代码库.
我的问题是,导入的最佳方式是什么,Observable所以我可以使用该toPromise()功能而无需导入其他所有内容?
我有两个服务。login.service.ts和environment-specific.service.ts。
在login.service.ts我需要初始化一个 Json 对象,environment-specific.service.ts从中传递几个链接。
用例是如何在没有 的情况下使其在 servcie 中工作ngOnInit(){},因为 ngOnInit()根据 Angular2 Lifecycle Hook 不能在 servcie 中使用。
在这里你可以找到我必须在里面初始化的部分login.service.ts- 当然没有ngOnInit()方法:
...
import { EnvSpecific } from '../models/env-specific';
import { ActivatedRoute } from "@angular/router";
@Injectable()
export class LoginService {
link1: string;
link2: string;
link3: string;
constructor(private http: Http, private fbuilder:FormBuilder, private route: ActivatedRoute) { }
ngOnInit(){
this.route.data
.subscribe((data: { envSpecific: EnvSpecific }) => {
this.link1 = data.envSpecific.link1;
this.link2 = …Run Code Online (Sandbox Code Playgroud) 我想在角度2应用程序的起点做一些事情,但我无法得到那一点.如果有人知道这件事,请帮助我.
提前致谢.
我试图弄清楚如何在 Angular 11 中使用useFactory作为异步函数。现在我有这个:
import { ApolloClientOptions } from 'apollo-client';
import { FirebaseService } from './firebase.service';
// other imports here...
export async function createApollo(httpLink: HttpLink, fs: FirebaseService): Promise<ApolloClientOptions<any>> {
const token = await fs.getToken();
const getHeaders = async () => {
return {
"X-Auth-Token": token,
};
};
// functions has more content here...
return Promise.resolve({
link: errorLink.concat(link),
cache: new InMemoryCache(),
});
}
@NgModule({
imports: [HttpLinkModule],
providers: [
{
provide: APOLLO_OPTIONS,
useFactory: createApollo,
deps: [HttpLink, FirebaseService],
},
],
}) …Run Code Online (Sandbox Code Playgroud) async-await angular-providers angular-module angular angular11
APP_INITIALIZER 运行一系列嵌套的承诺(我尝试过订阅,结果没有任何差异)。
APP_INITIALIZER 在从 API 服务器检索数据之前需要进行身份验证。它还需要从 API 服务器拉取两个表(按顺序)。
在 api.service 中,http/get 授权发生在一个承诺中。在承诺(then)之后,我去从API服务获取数据。
问题是组件 ngOnInit() - 它尝试在变量存在之前获取它们。
我已在组件中尝试了以下代码,但所做的只是调用 initData() 两次。
this.people = await this.apiService.initData();
Run Code Online (Sandbox Code Playgroud)
api.服务:
async initData(): Promise<Person[]> {
this.userData$ = this.http.get('/.auth/me', {observe: 'response'});
this.userData$.toPromise().then( res => {
this.resBody = res.body;
this.token = res.body[0].access_token;
this.getLegalSub(this.legalsubdeptsURL)
.toPromise().then(legalsubdepts => {
this.legalsubdepts = legalsubdepts;
this.getPeopleData(this.personURL)
.toPromise().then(people => {
this.people = people;
return this.people;
});
});
});
}
return this.people;
}
Run Code Online (Sandbox Code Playgroud)
应用程序模块
export function initData(appInitService: APIService) {
return (): Promise<any> => {
return appInitService.initData();
} …Run Code Online (Sandbox Code Playgroud) 我正在使用 Angular 7 构建一个应用程序,我已经处理了 API 调用、使用 C# 的 JWT 令牌身份验证系统,并在必要时、用户登录和注销时更新了 LocalStorage(),所有这些都运行良好。
我的问题是我希望它作为应用程序中的中间件而不是在生命周期方法上运行登录检查 - ng.onInit()。我该怎么办?
有没有办法将生命周期事件作为入口组件或服务执行。也就是说,在加载任何组件之前,它能够检查用户是否登录并通过路由器重定向到所需的页面。