如何在角度2中获取主机元素参考?在我的情况下,我想知道在我的组件中是否有焦点.可能吗?
最好的祝福!
我正在尝试使用 redux(ngrx) 创建一个“身份验证应用程序”,并且我正在尝试在秘密守卫中使用我的应用程序状态。在这里你可以看到我的 github:https : //github.com/tamasfoldi/ngrx-auth/tree/router 这是我的守卫的样子:
@Injectable()
export class SecretGuard implements CanActivate {
constructor(private store: Store<AppState>, private router: Router) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.store.let(getLoginState())
.map(state$ => state$.isLoggedIn)
}
}
Run Code Online (Sandbox Code Playgroud)
它返回 isLoggedIn 属性,这应该没问题,因为路由器解析了 promises 和 observable,但是当我导航到秘密部分时路由器阻止了它。以下是我的路线:
export const appRoutes: Routes = [
{
path: '',
redirectTo: 'auth',
pathMatch: 'full'
},
{
path: 'auth',
children: [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent …Run Code Online (Sandbox Code Playgroud) 我目前有一个容器(有状态)组件,它根据方法中的路由参数(id)调度一个select和一个get动作ngOnInit。这些操作的要点是在我的商店中拥有数据和选定的 id。
我很好奇在解析器中分派这些动作是否正确?
感谢您的回复。
我的组件:
@Component({
selector: 'app-container',
templateUrl: './container.component.html',
styleUrls: ['./container.component.css']
})
export class ContainerComponent implements OnInit, OnDestroy {
private componetDestroyed$ = new Subject();
constructor(private store: Store<fromRoot.State>, private route: ActivatedRoute) { }
ngOnInit() {
this.route.params
.filter(params => params['id'])
.map(params => params['id'])
.takeUntil(this.componetDestroyed$)
.subscribe(id => {
this.store.dispatch(new GetAction(id));
this.store.dispatch(new SelectAction(id));
});
}
ngOnDestroy() {
this.componetDestroyed$.next();
this.componetDestroyed$.unsubscribe();
}
}
Run Code Online (Sandbox Code Playgroud)
我的路线:
[{
path: ':id',
component: ContainerComponent
}]
Run Code Online (Sandbox Code Playgroud)
解析器将是:
@Injectable()
class MyResolver implements Resolve<any> {
constructor(private store: …Run Code Online (Sandbox Code Playgroud) 当组件输入更改时,是否可以告诉 Angular 运行 init (或任何其他生命周期函数或函数),或者我必须从父组件调用该函数而不是更改输入?
感谢您的回答!