如果在路由器中使用子組件,並通過綁定更新它們,我該如何共享變量?Angular 2 Dart:如何在父路由器和子組件之間共享變量?
這是(部分)我的代碼:
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);
}
}
總之我想從內ProfileComponent訪問isUserLoggedIn的當前值。我怎樣才能做到這一點?