简体   繁体   English

Angular CRUD - 添加/更新/删除操作后刷新 UI

[英]Angular CRUD - Refresh UI after add/update/delete operation

I was wondering that it is better to refresh the UI when executing an add/update/delete operation from a CRUD in Angular.我想知道在 Angular 中从 CRUD 执行添加/更新/删除操作时刷新 UI 会更好。

The options are as follows (for example, for delete action):选项如下(例如,对于删除操作):

1. Update local variable with previously fetched data after perform the action. 1.执行操作后用先前获取的数据更新局部变量。

deleteProduct(id) {
    this.productsService.deleteProduct(id).subscribe(status => {
        this.products = this.products.filter(item => item.id != id);
    });
}

2. Fetch the items again calling REST API. 2.再次调用 REST API 获取项目。

deleteProduct(id) {
    this.productsService.deleteProduct(id).subscribe(status => {
        this.getProducts();
    });
}

getProducts() {
    this.productsService.getProducts().subscribe(products => {
        this.products = products;
    });
}

This would also be applicable to add or edit, with more code obviously.这也适用于添加或编辑,显然有更多的代码。

Which option is better in terms of good practices and performance?哪个选项在良好实践和性能方面更好? Updating variable data locally or performing a new http request to obtain updated data.在本地更新变量数据或执行新的 http 请求以获取更新的数据。

第一种方法更受欢迎,它比第二种方法耗时少,因此加载时间很短。

I think, It can depend on your project .我认为,这取决于您的项目。 I use the following flow in my project (client[ Angular ] should follow the server)我在我的项目中使用以下流程(客户端[ Angular ]应该跟随服务器)

(Here I use WEB API ) (这里我使用WEB API

To create : It's better to return the created Item as below创建:最好返回创建的项目,如下所示

[HttpPost]
public ActionResult Post(Item item)
{
    if (everything is ok)
    {
        // resourceUrl: the address of the source that we have just created
        return Created(resourceUrl, item);
    }

    if (there is an error) {
        return BadRequest();
    }
}

To Edit : You do not need to return any Item, you should return the status code ( 400 [BadRequest], 200 [OK], 204 [NoContent] and so on) like the following :编辑:您不需要返回任何项目,您应该返回状态代码( 400 [BadRequest]、 200 [OK]、 204 [NoContent] 等),如下所示:

[HttpPut("{id}")]
public ActionResult Put(Item item, int id)
{
    var existingItem = (retrive item form db based on id); 
    if (existingItem == null) {
        return BadRequest("Cannot update ...");
    } else {
        // update operation here ...
        return Ok();
    }
}

To delete - you should return the status code ( 400 [BadRequest], 200 [OK], 204 [NoContent] and so on)要删除- 您应该返回状态代码( 400 [BadRequest]、 200 [OK]、 204 [NoContent] 等)

EDIT:编辑:

You don't have to follow above recommendation.您不必遵循上述建议。 You can select the first suggestion in your question ( Update local variable with previously fetched data after perform the action .) , It depends on your project.您可以选择问题中的第一个建议(执行操作后使用先前获取的数据更新局部变量。),这取决于您的项目。

I think it depends on your application.我认为这取决于您的应用程序。

Obviously the first approach is one less api call and is faster.显然,第一种方法是更少的 api 调用并且速度更快。

But imagine you have a high frequency of operations on the backend data by other users.但想象一下,其他用户对后端数据的操作频率很高。 In that case it could be very useful to refresh the list after an operation.在这种情况下,在操作后刷新列表可能非常有用。

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

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