gpt4 book ai didi

angular - 如何从 ngrx 商店中获取值(value)到我的模板中?

转载 作者:行者123 更新时间:2023-12-04 00:05:48 24 4
gpt4 key购买 nike

这里是一个完全的 Redux 新手,我很难将我的数据从商店中取出以便在我的 View 中使用。这是我的 Actions、Reducers 等。

流派.model.ts

export interface Genre {
id: string;
title: string;
description: string;
slug: string;
error: string;
}

export const initialState: Genre = {
id: '',
title: '',
description: '',
slug: '',
error: null
};

流派.service.ts
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Http } from '@angular/http';
import {environment} from "../../../environments/environment";
import {Genre} from "./genre.model";

@Injectable()
export class GenreService {

apiUrl = environment.apiUrl + environment.apiVersion + '/';

constructor(private http: Http) { }

/**
* Gets Genres
*
* @returns {Observable<Genre[]>}
*/
getGenres(): Observable<Genre[]> {
return this.http.get(this.apiUrl + 'genres/')
.map(res => res.json());
}

/**
* Gets an individual genre
*
* @param {String} slug
* @returns {Observable<Genre[]>}
*/
getGenre(slug: String): Observable<Genre[]> {
return this.http.get(this.apiUrl + 'genres/' + slug)
.map(res => res.json().genre);
}
}

流派.actions.ts
import { Action } from '@ngrx/store';
import { Injectable } from '@angular/core';

import { Genre } from '../_shared/genre.model';

@Injectable()
export class GenreActions {

static LOAD_GENRES = '[Genre] Load Genres';
loadGenres(): Action {
return {
type: GenreActions.LOAD_GENRES
};
}

static LOAD_GENRES_SUCCESS = '[Genre] Load Genres Success';
loadGenresSuccess(genres): Action {
return {
type: GenreActions.LOAD_GENRES_SUCCESS,
payload: genres
};
}

static GET_GENRE = '[Genre] Get Genre';
getGenre(slug): Action {
return {
type: GenreActions.GET_GENRE,
payload: slug
};
}

static GET_GENRE_SUCCESS = '[Genre] Get Genre Success';
getGenreSuccess(genre): Action {
return {
type: GenreActions.GET_GENRE_SUCCESS,
payload: genre
};
}
}

流派.reducers.ts
import { Action } from '@ngrx/store';

import {Genre, initialState} from '../_shared/genre.model';

import { GenreActions } from './genre.actions';

export function genreReducer(state: Genre = initialState, action: Action) {
switch (action.type) {
case GenreActions.GET_GENRE_SUCCESS: {
return action.payload;
}
case GenreActions.LOAD_GENRES_SUCCESS: {
return action.payload;
}
default: {
return state;
}
}
}

流派.效果.ts
export class GenreEffects {
constructor (
private update$: Actions,
private genreActions: GenreActions,
private svc: GenreService,
) {}

@Effect() loadGenres$ = this.update$
.ofType(GenreActions.LOAD_GENRES)
.switchMap(() => this.svc.getGenres())
.map(genres => this.genreActions.loadGenresSuccess(genres));

@Effect() getGenre$ = this.update$
.ofType(GenreActions.GET_GENRE)
.map(action => action.payload)
.switchMap(slug => this.svc.getGenre(slug))
.map(genre => this.genreActions.getGenreSuccess(genre));
}

流派.detail.component.ts
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import { ActivatedRoute, Router } from '@angular/router';
import { Component, OnInit } from '@angular/core';

import { Genre } from '../_shared/genre.model';
import { GenreActions } from '../_store/genre.actions';

@Component({
selector: 'app-genre-detail',
templateUrl: './genre-detail.component.html',
styleUrls: ['./genre-detail.component.scss']
})
export class GenreDetailComponent implements OnInit {

public idSub: Subscription;
public genre: Observable<any>;

constructor(
private store: Store<Genre>,
private route: ActivatedRoute,
private genreActions: GenreActions,
private router: Router
) {
this.genre = store.select('genres');
}

ngOnInit() {
this.idSub = this.route.params.subscribe(params => {
this.store.dispatch(this.genreActions.getGenre(params['slug']));
console.log(this.genre);
});
}
}

我可以看到我的 API 请求被触发,它是返回数据,我可以在 Redux devtools 中看到正在填充状态,但我似乎无法使用正常的 {{ genre.title }} 将数据输出和输入到我的 View 中我只是得到 [Object object] 被扔回给我?

我确信这可能是一件非常容易的事情,但就像我说的那样,我是一个完全的菜鸟,并且花了大约 5 个小时来尝试不同的东西,遵循不同的教程等。

Console

最佳答案

我猜你的流派是一个数组列表

应该是这样的,把它当作线框。

genre : any ;

ngOnInit(){
this.idSub = this.route.params.subscribe(params => {
this.store.dispatch(this.genreActions.getGenre(params['slug']));
});
this.store.select('genres').subscribe(data => this.genre = data)
}

如果您想查看ngrx 4,请查看 link

我刚刚挖出了我的 git,你可以查看我使用 ngrx v2 的 repo 的快照。我没有相同的工作示例,但请放心,代码有效 LINK

更新

为 Genre 制作一个不同的 Object 以在 state 中使用 Genre 接口(interface)
export interface AppState {
genre:Genre
}

现在在构造函数或 ngOnInit 中订阅此状态对象类型
private store: Store<AppState>, 
private route: ActivatedRoute,
private genreActions: GenreActions,
private router: Router
) {
this.store.select('genre').subscribe(data => this.genre = data);
}

关于angular - 如何从 ngrx 商店中获取值(value)到我的模板中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45718951/

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