TypeScript编译器错误地报告action.payload不存在

时间:2017-09-13 19:22:18

标签: typescript ngrx ngrx-store-4.0

我使用的是Ngrx 4.03和typeScript 2.5.2。

我已经定义了一个动作:

import { Action } from '@ngrx/store';
import { IUser } from '../models';

export const LOGIN_SUCCESS = '[Auth] Login Success';
export const LOGOUT_SUCCESS = '[Auth] Logout Success';

export class LoginSuccess implements Action {
  readonly type = LOGIN_SUCCESS;

  constructor (
    public payload: IUser
  ) {}
}

export class LogoutSuccess implements Action {
  readonly type = LOGOUT_SUCCESS;
}

export type AuthActions
 = LoginSuccess
 | LogoutSuccess;

在我相应的reducer中,TypeScript告诉我action.payloadAction不存在,即使我可以正确记录该值。我做错了什么?

2 个答案:

答案 0 :(得分:0)

我几乎可以肯定,如果您有一个没有有效负载的操作(或更多),它将引入此行为。

所以改变这段代码:

<?php
namespace Webkul\Grid\Observer;

use Magento\Framework\Event\ObserverInterface;

class coupenAppliedAfter implements ObserverInterface
{
    /**
     * @var ObjectManagerInterface
     */
    protected $_objectManager;

    /**
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     */
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager
    ) {
        $this->_objectManager = $objectManager;
    }

    /**
     * customer register event handler
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // get enetered coupen code
        $controller = $observer->getControllerAction();
        $couponCode = $controller->getRequest()->getParam('coupon_code');

        $objectManager =   \Magento\Framework\App\ObjectManager::getInstance();
        $connection = $objectManager->get('Magento\Framework\App\ResourceConnection')->getConnection('\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION'); 
        // get list of coupon codes from that custom table
        $all_custom_codes = $connection->fetchAll("SELECT * FROM custom_promotion_rules");

        foreach($all_custom_codes as $code) {
            $db_coupen_code = $code['code'];
            // matching if user has entered any custom code 
            if($couponCode == $db_coupen_code) {
                // if yes trying to apply custom discount
                $DiscountAmount = $code['discount_amount'];
                $result = $observer->getEvent()->getResult();
                $result->setAmount($DiscountAmount);
                $result->setBaseAmount($DiscountAmount); 
            }
        }

    }
}

Fatal error: Uncaught Error: Call to a member function setAmount() on null

答案 1 :(得分:0)

因此,在商店版本4中,删除了对操作中的有效负载的支持,因为您已经看到已经报告了问题。 https://github.com/ngrx/platform/issues/31

您可以通过重写此类操作来消除此问题。

import { Action } from '@ngrx/store';
export interface CustomAction extends Action {
  payload: any;
  appraisal_id: any;
}