Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Dart: How to share variables between parent with router and child components?

How do I share variables if having a child component with a router and keep them updated through bindings?

This is (part of) my code:

app_component.dart:

import 'package:angular2/core.dart';
import 'package:angular2_components/angular2_components.dart';
import 'package:angular2/router.dart';

import 'package:my_app/profile_component/profile_component.dart';
import 'package:my_app/login_component/login_component.dart';

@Component(
  selector: 'my-app',
  styleUrls: const ['app_component.css'],
  templateUrl: 'app_component.html',
  directives: const [
    ROUTER_DIRECTIVES,
    materialDirectives,
    LoginComponent,
    ProfileComponent
  ],
  providers: const [
    ROUTER_PROVIDERS,
    const Provider(LocationStrategy, useClass: HashLocationStrategy),
    materialProviders
  ],
)
@RouteConfig(const [
  const Route(path: 'home', name: 'Home', component: HomeComponent, useAsDefault: true),
  const Route(path: 'profile', name: "User Profile", component: ProfileComponent)
])
class AppComponent {

  @ViewChild('login')
  LoginComponent loginComponent;
  @ViewChild('profile')
  ProfileComponent profileComponent;

  bool get isUserLoggedIn => loginComponent.isUserLoggedIn;

}

app_component.html:

<nav>
    <ul>
    <li *ngIf="!isUserLoggedIn">
        <a (click)="loginComponent.loginOpen=true">Account / Login</a>
    </li>
    <li *ngIf="isUserLoggedIn">
        <a id="mb_logout" (click)="loginComponent.logUserOut()">Logout</a>
    </li>
    <li *ngIf="isUserLoggedIn">
        <a id="mb_profile"  [routerLink]="['User Profile']">Zum Profil</a>
    </li>
    </ul>
</nav>

login_component.dart:

...
bool get isUserLoggedIn => _isUserLoggedIn();
...

profile_component.dart:

import 'package:angular2/core.dart';
import 'package:angular2_components/angular2_components.dart';

@Component(
  selector: 'profile',
  styleUrls: const ['profile_component.css'],
  templateUrl: 'profile_component.html',
  directives: const [materialDirectives],
  providers: const [materialProviders],
)
class ProfileComponent {

  @Input()
  bool isUserLoggedIn;

  ProfileComponent() {
    print(isUserLoggedIn);
  }
}

In short I want to access the current value of isUserLoggedIn from within ProfileComponent. How can I achieve this?

like image 814
Blackbam Avatar asked Jan 04 '23 01:01

Blackbam


1 Answers

On the parent component provide a shared service

@Injectable()
class SharedService {
  bool _isUserLoggedIn = false;
  bool get isUserLoggedIn => _isUserLoggedIn;
  set isUserLoggedIn(bool value) {
    _isUserLoggedIn = value ?? false;
    _isUserLoggedInController.add(_isUserLoggedIn);
  }

  StreamController<bool> _isUserLoggedInController;
  Stream<bool> get onIsUserLoggedIn => _isUserLoggedInController.stream;


  SharedService() {
     _isUserLoggedInController = new StreamController<bool>.broadcast(
       onListen: () => _isUserLoggedInController.add(_isUserLoggedIn)
     );
  }
}

In components where you want to update use or update the status inject the service

@Component(
  ...,
  template: '''
    <div>isLoggedIn: {{sharedService.onIsUserLoggedIn | async}}</div>
    <button (click)="toggleIsUserLoggedIn()">{{(sharedService.onIsUserLoggedIn | async) ? 'log out' : 'log in'}}</div>
  ''',
)
class SomeComponent {
  final SharedService sharedService;
  SomeComponent(this.sharedService);

  toggleIsUserLoggedIn() {
    sharedService.isUserLoggedIn = !sharedService.isUserLoggedIn;
  }
}

Provide the service in AppComponent

@Component(
  selector: 'my-app',
  providers: const [SharedService],
)
class AppComponent {}
like image 122
Günter Zöchbauer Avatar answered Jan 10 '23 21:01

Günter Zöchbauer