简体   繁体   English

Angular2 NGRX / Store视图未更新

[英]Angular2 NGRX/Store View Not Updating

Sorry for yet another Angular 2 view update question but I haven't been able to find an answer in any other responses. 对于另一个Angular 2视图更新问题,我很抱歉,但我无法在任何其他回复中找到答案。 I'm mentioning the use of NGRX/Store here though I doubt its the reason the view isn't updating.Oddly enough it seems to update once (at least) but manages to stay a step behind. 我在这里提到NGRX / Store的使用,虽然我怀疑它没有更新视图的原因。足够的它似乎更新一次(至少)但是设法保持落后一步。 The last (most recent) update never takes affect. 最后一次(最近的)更新永远不会生效。 When the model updates, the previous state updates in the view. 模型更新时,先前的状态会在视图中更新。 It's mind boggling really. 真的令人难以置信。 Heres the appropriate code. 下面是适当的代码。

Relevant Installed packages 相关的已安装包

    "@angular/common": "2.0.0-rc.5",
    "@angular/compiler": "2.0.0-rc.5",
    "@angular/core": "2.0.0-rc.5",
    "@angular/forms": "^0.3.0",
    "@angular/http": "2.0.0-rc.5",
    "@angular/platform-browser": "2.0.0-rc.5",
    "@angular/platform-browser-dynamic": "2.0.0-rc.5",
    "@angular/router": "^3.0.0-rc.1",
    "@angular/upgrade": "2.0.0-rc.5",
    "@ngrx/core": "^1.0.1",
    "@ngrx/store": "^2.0.1",

My main app module 我的主要应用模块

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        RouterModule.forRoot(AppRoutes)
    ],
    declarations: [
        ConsumerApp,
        AppManagement,
        MyApps,
        AppRegistration,
        AppDetails,
    ],
    providers: [
        HTTP_PROVIDERS,
        STORE_PROVIDERS,
        AppManagementService,
    ],
    bootstrap: [
        ConsumerApp
    ]
})
export class AppModule { }

My-Apps Component 我的应用程序组件

@Component({
    selector: 'my-apps',
    template: require('./templates/my-apps.html')
})
export class MyApps implements OnInit, OnDestroy {

    apps = {};
    change = 1;
    userAppsSubscription;

    constructor (
        private appsService: AppManagementService,
        private store: Store<any> ) {}

    ngOnInit () {

        this.userAppsSubscription = this.store
            .select('userApps')
            .subscribe( userApps => {
                this.apps = Object.assign(this.apps, userApps);
                this.change++;
                console.log(this);
            })

        this.appsService.getUserApps();
    }

    ngOnDestroy () {

        this.userAppsSubscription.unsubscribe();
    }
}

My-Apps template 我的应用模板

<div id="my-apps">
    <h1>My Apps</h1>
    <h2>{{change}}</h2>
    <p>You currently don't have any registered applications. Click "Register an App" to get started.</p>

    <p [hidden]="!loading">{{apps | json}}</p>

    <div class="apps-container">  
        <div class="col-md-4 col-sm-6" *ngFor="let app of apps.available">
            <button class="block-button">
                <h3>{{app.name}}</h3>
                <p><strong>Description:</strong> {{app.description}}</p>  
            </button>
        </div>
    </div>

</div>

App Management Service 应用管理服务

@Injectable()
export class AppManagementService {

    constructor (
        private http: Http, 
        private store: Store<any>) {}

    getUserApps () {

        this.store
            .dispatch({
                type: RETRIEVE_USER_APPS,
                payload: null
            })

        return this.http
            .get(ALL_APPS)
            .toPromise()
            .then(response => {
                this.store
                    .dispatch({
                        type: RECIEVE_USER_APPS,
                        payload: response.json()
                    })
            })
            .catch(response => {
                this.store
                    .dispatch({
                        type: RETRIEVAL_FAILURE_USER_APPS,
                        payload: null
                    })
            });
    }
}

When the "My Apps" page is first hit this is what end up being printed out. 首次点击“我的应用”页面时,最终会打印出来。

在此输入图像描述

And this is where the view end up. 这就是观点最终的结果。

在此输入图像描述

Odd right? 奇怪吧? you can see the page was updated with the second state broadcasted from the NGRX/Store but not the final state Which contains the real bread and butter (the list of 6 apps). 您可以看到页面已更新为NGRX / Store播出的第二个状态,但不包含最终状态,其中包含真正的面包和黄油(6个应用程序列表)。 Why on earth would the view not update properly? 为什么视图不能正确更新?

Can you post your reducer? 你能发布减速机吗?

I would refactor this to not unwrap the stream in the Init method. 我会重构这个,以便不在Init方法中解包流。 Use the async pipe to do this for you. 使用异步管道为您执行此操作。 This also handles the unsubscribe. 这也处理取消订阅。

Something like: 就像是:

HTML HTML

<div id="my-apps">
    <h1>My Apps</h1>
    <h2>{{change}}</h2>
    <p>You currently don't have any registered applications. Click "Register an App" to get started.</p>

    <p [hidden]="!loading">{{apps | json}}</p>

    <div class="apps-container">  
        <div class="col-md-4 col-sm-6" *ngFor="let app of ($userAppsSubscription | async).apps.available">
            <button class="block-button">
                <h3>{{app.name}}</h3>
                <p><strong>Description:</strong> {{app.description}}</p>  
            </button>
        </div>
    </div>
</div>

TS TS

@Component({
    selector: 'my-apps',
    template: require('./templates/my-apps.html')
})
export class MyApps implements OnInit, OnDestroy {

    apps = {};
    change = 1;
    $userAppsSubscription:Observable<string>;

    constructor (
        private appsService: AppManagementService,
        private store: Store<any> ) {}

    ngOnInit () {

        this.userAppsSubscription = this.store
            .select('userApps');

        this.appsService.getUserApps();
    }

    ngOnDestroy () {

        this.userAppsSubscription.unsubscribe();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM