【发布时间】:2018-03-25 20:45:05
【问题描述】:
我正在做一个非常简单的 NgRx 实现,现在只是为一个组件做 get。根据 Redux Devtool,数据来自有效负载,但我似乎无法通过异步管道访问它。
开发工具:
---更新----
问题是当我访问我的 ts 文件中的切片时,我得到了错误
“网络”类型的参数不能分配给类型参数 '“开始”'。
ts 文件
constructor(private store: Store<fromApp.AppState>) {}
getDns() {
// reach out to ngrx store
this.dnsServers$ = this.store.select('network').map(network => network.dns)
console.log(this.dnsServers$)
// get current dns IPs'
this.networkService.getDns()
.subscribe(
(dnsList: Dns) => {
console.log(dnsList.dns);
this.store.dispatch(
new LoadIpSuccessAction(dnsList.dns)
)
}
)
}
我的功能模块
StoreModule.forFeature('network', { dns: dnsReducer })
我的根减速器(我没有任何模块没有延迟加载,所以我的根减速器是空的......不知道如何让它工作)
import { Action } from '@ngrx/store';
export interface State {
start: string;
}
const initialState: State = {
start: ''
}
export function startReducer(state = initialState, action: Action) {
return state;
}
和我更新的功能缩减器来访问 dns 数组
import * as NetworkActions from './network.actions';
import * as fromApp from '../../store/app.reducers';
export interface NetworkState extends fromApp.AppState {
dns: DnsState;
}
export interface DnsState {
dns: string[];
}
const initialState: DnsState = {
dns: []
}
export function dnsReducer(state = initialState, action: NetworkActions.DnsActions) {
switch (action.type) {
case NetworkActions.LOAD_IP_SUCCESS:
return {
...state,
dns: [...action.payload]
}
default:
return state;
}
}
【问题讨论】:
-
尝试在 getDns() 之外设置 dnsServer = this.store.select('dns') 的定义。如果您使用类型,还要将 dnsServer 的类型设置为 Obseravble。
-
getDns() 在 ngOninit() 中被调用,所以它是否在外面无关紧要。我最终会输入可观察的,但这并不能解决问题
-
我想我知道。返回的有效负载对象可能与订阅获取的值不同,然后立即设置。最初可能只是空的,并且 async 什么也得不到,但在调度后会更新。但是,由于期望未定义的 DNS,您会导致它出错。因此,为什么它在没有安全操作员的情况下会失败,但可以在安全的情况下工作。这些值稍后会更新为工作值。如果您将默认状态设置为具有 DNS 和一些假值的对象,它将起作用....试试吧!