gpt4 book ai didi

angular - 如何找出是什么破坏了 Angular 绑定(bind)?

转载 作者:太空狗 更新时间:2023-10-29 18:09:21 26 4
gpt4 key购买 nike

我有 Angular app支持具有 2 个绑定(bind)的多个浏览器选项卡,其他使用 angular-redux @select 和另一个 {{ property }}。该应用程序按预期工作。但是,我可以通过使用 redux-state-sync 中间件配置我的 Angular 存储来使用广播 channel 而不是本地存储来打破绑定(bind)。因此,在 app.component.ts 中用其下方的注释行替换一行会破坏第二个浏览器窗口中的绑定(bind)。这看起来真的很奇怪,我不知道如何找出为什么这两个绑定(bind)会因看似无关的更改而中断。

编辑:引用 yurzui 的回答:状态也通过广播 channel 选项在选项卡间同步。只有绑定(bind)不再起作用。当按下按钮时,可以在不同浏览器选项卡的控制台输出中验证这一点。

app.component.html

<div style="text-align:center">
<div *ngIf="(isLoggedIn | async)">
<p>this appears when logged in!</p>
</div>
<h1>
App status is {{ statusTxt }}!
</h1>
<button (click)="toggleLogin()">{{ buttonTxt }}</button>
<h2>You can either login 1st and then open another tab or you can 1st open another tab and then login. Either way the both tabs should be logged in and look the same</h2>
<h2>After testing multiple tabs with current 'localstorage' broadcastChannelOption type, you can change it to 'native' in app.component.ts and you'll see that bindings do not work anymore</h2>
</div>

应用程序组件.ts

import { Component, Injectable } from '@angular/core';
import { createStateSyncMiddleware, initStateWithPrevTab, withReduxStateSync } from 'redux-state-sync';
import { combineReducers, Action, createStore, applyMiddleware } from 'redux';
import { NgRedux, select } from '@angular-redux/store';
import { Observable } from "rxjs";

export interface LoginToggle {
isLoggedIn : boolean
}

export interface LoginToggleAction extends Action {
loginToggle: LoginToggle;
}

@Injectable()
export class LoginActions {
static TOGGLE_LOGIN = 'TOGGLE_LOGIN';

toggleLogin(loginToggle: LoginToggle): LoginToggleAction {
return {
type: LoginActions.TOGGLE_LOGIN,
loginToggle
};
}
}

export interface ILoginState {
readonly isLoggedIn: boolean;
}

export interface IApplicationState {
login: ILoginState;
}

export const INITIAL_STATE : IApplicationState = {
login : { isLoggedIn: false }
}

export function loginReducer(oldState: ILoginState = { isLoggedIn : false } as any, action: Action) : ILoginState {
switch (action.type) {
case LoginActions.TOGGLE_LOGIN: {
console.log('in reducer');
const toggleAction = action as LoginToggleAction;
return {
...oldState,
isLoggedIn: toggleAction.loginToggle.isLoggedIn
} as ILoginState;
}
default: {
return oldState;
}
}
}

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
statusTxt = 'logged out';
subscription;
loginStatus = false;
buttonTxt = 'Login';

@select((state: IApplicationState) => state.login.isLoggedIn) isLoggedIn: Observable<boolean>;

constructor(private ngRedux: NgRedux<IApplicationState>,
private actions : LoginActions) {

const appReducer = combineReducers<IApplicationState>({
login: loginReducer
})

const rootReducer = withReduxStateSync(appReducer);
const store = createStore(rootReducer,
applyMiddleware(createStateSyncMiddleware({ broadcastChannelOption: { type: 'localstorage' } })));

/* !! Both bindings break if i replace the row above with this:
applyMiddleware(createStateSyncMiddleware())); */

initStateWithPrevTab(store);

ngRedux.provideStore(store);

this.subscription = this.ngRedux.select<ILoginState>('login')
.subscribe(newState => {
console.log('new login state = ' + newState.isLoggedIn);
this.loginStatus = newState.isLoggedIn;
if (newState.isLoggedIn) {
this.statusTxt = 'logged in!';
this.buttonTxt = 'Logout';
}
else {
this.statusTxt = 'logged out!';
this.buttonTxt = 'Login';
}
console.log('statusTxt = ' + this.statusTxt);
});
}

toggleLogin(): void {
this.ngRedux.dispatch(this.actions.toggleLogin({ isLoggedIn: !this.loginStatus }));
}
}

最佳答案

当您使用 localstorage channel 时,您的应用程序订阅了浏览器 storage事件。一旦您的应用程序更改了一个选项卡中的值,它就会更新 locastorage,这会触发其他选项卡中的 storage 事件,因此它会被更新。

使用默认的 redux_state_sync channel ,您在选项卡之间没有这种连接,但 channel 现在使用 BroadcastChannel 未由 zonejs 修补,因此您始终会收到不在 Angular 区域中的消息。

你能做什么?

订阅时在 Angular 区域内手动运行代码:

constructor(...private zone: NgZone) {

this.subscription = this.ngRedux.select<ILoginState>('login')
.subscribe(newState => {
this.zone.run(() => { // enforce the code to be executed inside Angular zone
console.log('new login state = ' + newState.isLoggedIn);
this.loginStatus = newState.isLoggedIn;
...
})
});

请注意,您也可以运行 cdRef.detectChanges,但在这种情况下,您可能会遇到一些 Angular 处理程序将在 Angular 区域之外注册的情况因此,您将在其他地方调用 detectChanges 或再次使用 zone.run

所以我建议你使用 zone.run 来确保你在 Angular 应用程序中的正确区域

关于angular - 如何找出是什么破坏了 Angular 绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56162064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com