Aurelia国家管理

问题描述 投票:4回答:5

aurelia有没有最佳实践或国家管理图书馆?像vue.js的综合状态管理?

我从一个简单的对象看到了一些建议:

export default {
user: ...,
router: ...,
...,
}

一些aurelia redux集成:https://github.com/steelsojka/aurelia-redux-plugin

有没有人通过一个复杂程度较低的图书馆获得良好的经验?在我看来,Redux是一个很好的样板。我想要一些封装,其占地面积较小,类似于连身衣的反应。

我曾多次尝试过:

store.js

存储为对象:

export default{
  test: 'xxx'
};

作为班级存储:

export default class Store{
  constructor() {
    this.test = 'xxx';
  }
};

在组件中,我将它集成如下:

import { inject } from 'aurelia-framework';
import store from './store';

@inject(store)
export class TestComp {
  constructor(store){
    this.store = store;
  }
}

甚至没有注入:

import store from './store';

export class TestComp {
  constructor(){
    this.store = store;
  }
}

所有组合似乎都很好,商店在视图/组件之间保持同步。

有什么(一个在另一个的缺点...或者你会建议完全不同的方法?

redux state aurelia mobx
5个回答
2
投票

另一种方法是继续使用经典服务,但只在操作中更改了商店中的单个状态。你可以在这里找到一篇关于它的文章http://pragmatic-coder.net/using-a-state-container-with-aurelia/。此方法在订阅的核心使用RxJS BehaviorSubject,以使组件与状态同步。除此之外,与使用redux和重写应用程序相比,它为现有应用程序提供了更简单的升级途径。哦,还包括Redux DevTools支持;)


2
投票

与此同时,Aurelia-Store是官方Aurelia国家管理插件,已正式发布。看看the docs,了解它是如何工作的。


0
投票

我已经去了类方法,然后在我需要的地方注入类。

import {observable} from 'aurelia-framework';
import {Cookie} from 'aurelia-cookie';

export class AppConfig {

  @observable companyCode;
  @observable applCodeInUse;
  @observable theme;

  constructor() {
    // Set up properties in here
  }

  // Some functions used for value conversion

}

然后,因为注入的类被视为单例 - 您可以只注入AppConfig类并访问属性。

import {inject, NewInstance, ObserverLocator} from 'aurelia-framework';
import {AppConfig} from 'config/AppConfig';

@inject(AppConfig)
export class Form {
    constructor(appConfig) {
       this.appConfig = appConfig;
    }
} 

这显然与您的解决方案非常相似。我使用了一个类,因为有一些用于转换值的业务逻辑等等。

您的怀疑似乎是关于这是否是糟糕的设计 - 但Aurelia的观点只要您的代码遵循标准,那么您可以自由地使用您喜欢的任何惯例来实现您的结果。据我所知,这种方法没有任何与最新的JS标准相悖的方法。它也轻巧灵活。我觉得没问题。


0
投票

我建议使用react-rest-state进行状态管理

它易于使用和高性能。

对于React用户来说,这是最好的做法

https://github.com/hosseinmd/react-rest-state#readme


-1
投票

您可以在http://patrickwalters.net/my-best-practices-in-aurelia/中阅读更多相关信息

我建议使用注射。

© www.soinside.com 2019 - 2024. All rights reserved.