缺少类型'Observable <PizzaState>的以下属性

时间:2019-12-27 15:12:06

标签: angular typescript rxjs

我尝试创建从商店收到但对它有视线的比萨饼。

this.pizzas$

我不知道为什么会这样,但是当我删除时 : Observable<PizzaState> 此修复程序,但我想使用强类型功能进行修复?

完整代码

import { Store } from '@ngrx/store';
import { ProductesState } from '../shared/models/productesState.model';
import { Observable } from 'rxjs';
import { PizzaState } from '../shared/models/pizzaState.model';

@Component({
  selector: 'app-read',
  templateUrl: './read.component.html',
  styleUrls: ['./read.component.css']
})
export class ReadComponent implements OnInit {

  public pizzas$: Observable<PizzaState>;

  constructor(private store: Store<ProductesState>) { }

  ngOnInit() {

    this.store.select('pizzas').subscribe(store => {
      this.pizzas$ = store;
    });

  }

}

我的减速器

import { PizzaState } from 'src/app/shared/models/pizzaState.model';
import * as fromPizzas from '../actions/pizzas.action'

export const initialState: PizzaState = {
    data: [],
    loaded: false,
    loading: false
}

export function reducer
    (state: PizzaState = initialState, action: fromPizzas.PizzasAction): PizzaState {

    switch (action.type) {

        case fromPizzas.LOAD_PIZZAS: {
            return {
                ...state,
                loading: true
            }
        }

        case fromPizzas.LOAD_PIZZAS_SUCCESS: {
            return {
                ...state,
                loaded: true,
                loading: false
            }
        }

        case fromPizzas.LOAD_PIZZAS_FAIL: {
            return {
                ...state,
                loaded: false,
                loading: false
            }
        }

        default: {
            return state;
        }

    }

}

和内部节点模块

    StoreModule.forRoot(
      { 'pizzas': reducers.pizzas }
    )

感谢帮助:-)

1 个答案:

答案 0 :(得分:2)

如果您希望它成为可观察对象,则不应订阅,因为订阅会将其从可观察对象中解包。您有两种选择:


public pizzas$: PizzaState;
...
this.store.select('pizzas').subscribe(store => {
  this.pizzas$ = store;
});


public pizzas$: Observable<PizzaState>;
...
this.pizzas$ = this.store.select('pizzas');