非常に悪いと私の英語のために申し訳ありません、私はロシアから来ました。私はあなたに尋ねたいと思う。私を助けてください。 私は4番のサービスを持っていますが、どのように多くのコンポーネントでそれを管理できますか?さまざまなコンポーネントでデータサービスを取得するにはどうすればよいですか?
は、これは私のサービスです:
import {EventEmitter, Injectable} from '@angular/core';
@Injectable()
export class AuthService {
status: boolean = false;
userAuth: EventEmitter<boolean> = new EventEmitter();
auth() {
this.status = true;
this.userAuth.emit(this.getAuth());
}
logout() {
this.status = false;
this.userAuth.emit(this.getAuth())
}
getAuth() {
return this.status;
}
}
これが私の最初のコンポーネントです。私はすでにngOnInitでサービスに登録しています
import {Component, OnInit} from '@angular/core';
import {ApiService} from "../../service/api/api.service";
import {BlockUI, NgBlockUI} from 'ng-block-ui'
import {AuthService} from "../../service/auth/auth.service";
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.less'],
providers: [ApiService, AuthService]
})
export class HeaderComponent implements OnInit {
isLogin: boolean = false;
@BlockUI() blockUI: NgBlockUI;
constructor(private as: ApiService, public authService: AuthService) {
this.blockUI.start();
this.as.isLogin().then((res) => {
if (res.hasError()) return false;
if (res.getResponse().login) this.authService.auth();
if (!res.getResponse().login) this.authService.logout();
// this.isLogin = this.authService.getAuth();
this.blockUI.stop();
});
}
ngOnInit() {
this.authService.userAuth.subscribe((status) => {
this.isLogin = status;
console.log('status', status);
console.log('this.isLogin', this.isLogin);
})
}
logout() {
this.as.logout().then((res) => {
if (res.hasError()) return false;
this.authService.logout();
// this.isLogin = this.authService.getAuth();
});
}
}
しかし、別のコンポーネントでサービスを呼び出すと、新しいサービスが呼び出されます。
あなたがサービスのデータを共有することはできませんが、あなたの特定のコンポーネントのみに特異的で、コンポーネントレベルでサービスを提供する場合、インスタンスをサービスし、角度がサービスのシングルトンオブジェクトを作成すると、あなたのメインモジュールにそれを追加する必要がありimport {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from "@angular/forms"
import {ApiService} from "../../service/api/api.service";
import {AuthService} from "../../service/auth/auth.service";
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.less'],
providers: [ApiService, AuthService]
})
export class LoginComponent {
formLogin: FormGroup;
constructor(private fb: FormBuilder, private as: ApiService, public authService: AuthService) {
this.formLogin = fb.group({
email: [null, Validators.compose([Validators.required, Validators.pattern(/^(([^<>()\[\]\\.,;:\[email protected]"]+(\.[^<>()\[\]\\.,;:\[email protected]"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)])],
password: [null, Validators.compose([Validators.required, Validators.minLength(6)])]
});
}
auth(form) {
this.as.authUser(form).then(res => {
if (res.hasError()) return false;
this.formLogin.reset();
this.authService.auth();
});
}
}
にNgModuleのプロバイダの配列ではなく、コンポーネントにあなたのサービスを追加します。 – n00dl3
はい、それは私を助けました。ありがとうございました:) –
この回答を見る-https://stackoverflow.com/questions/46044729/angular2-sharing-variable-or-global-variable-between-pages-components/46045167#46045167 –