对象解构的特殊行为

时间:2018-03-26 03:31:49

标签: typescript destructuring typescript2.7

我有以下内容:

  @Action( RsRosCoughUiUpdate )
  rsRosCoughUiUpdate( { getState, patchState }: StateContext<IRs>, payload: IFormState ) {
    console.log( `classToPlain(payload) | ${JSON.stringify( classToPlain(payload) ) }` )
    console.log('PAYLOAD', payload)
    console.log('PAYLOAD STATUS', payload.status)
    console.log('PAYLOAD STATUS', payload['status'])
    console.log( `change-before | ${JSON.stringify( payload.change ) }` )
    console.log(classToPlain(payload))
    console.log( `change-after | ${JSON.stringify( payload.change ) }` )

    let {change, dirty, pristine, status, target, touched, untouched} = payload

    console.log(change, dirty, pristine, status, target, touched, untouched)

    const nextState = produce( getState(), draftState => {
      let {change, dirty, pristine, status, target, touched, untouched} = payload
      console.log( `change | ${JSON.stringify( payload.change ) }` )
//       draftState.ros.ui.cough = payload



//      draftState.ros.ui.cough =    {...payload}
//      draftState.ros.ui.cough.change = payload.change
//      draftState.ros.ui.cough.dirty = payload.dirty
//      draftState.ros.ui.cough.pristine = payload.pristine
//      draftState.ros.ui.cough.status = payload.status
//      draftState.ros.ui.cough.target = payload.target
//      draftState.ros.ui.cough.touched = payload.touched
//      draftState.ros.ui.cough.untouched = payload.untouched
    } )

    patchState( { ...getState(), ...nextState } )
  }

当应用程序运行时,控制台日志如下所示:

classToPlain(payload) | {"payload":{"change":"add","dirty":true,"pristine":false,"status":"INVALID","target":"[cough->rs-ros]cmp","touched":true,"untouched":false}}
rs.store.ts:73 PAYLOAD RsRosCoughUiUpdate {payload: FormState}
rs.store.ts:74 PAYLOAD STATUS undefined
rs.store.ts:75 PAYLOAD STATUS undefined
rs.store.ts:76 change-before | undefined
rs.store.ts:77 {payload: {…}}payload: {change: "add", dirty: true, pristine: false, status: "INVALID", target: "[cough->rs-ros]cmp", …}__proto__: Object
rs.store.ts:78 change-after | undefined
rs.store.ts:82 undefined undefined undefined undefined undefined undefined undefined
rs.store.ts:86 change | undefined

删除所有调试信息并执行console.log(有效负载),提供以下内容

enter image description here

问题: 1.为什么破坏结构的值总是未定义的? 2.为什么有效载荷对象图的点遍历总是未定义?

干杯

1 个答案:

答案 0 :(得分:1)

如我的评论中所示,您可以在此处看到问题:

String file = "/sys/fs/cgroup/cpu,cpuacct/cpuacct.usage";
String content = new String(Files.readAllBytes(Paths.get(file)));

System.out.println(content); // 972897369764987

现在,如果您检查控制台结果,则会获得此日志:

console.log('PAYLOAD', payload);

它表示rs.store.ts:73 PAYLOAD RsRosCoughUiUpdate {payload: FormState}. payload,该对象具有RsRosCoughUiUdpate类型payload的属性。我假设FormState是符合您的FormState界面的类型。

所以,当然:

IFormState

显示console.log(payload.change);

要获得您的价值观,您的解构应该是:

undefined

也许您应该更改let {change, dirty, pristine, status, target, touched, untouched} = payload.payload 变量的名称,因为它不是您认为的。