Typescript and multiple classes

前端 未结 3 1191
既然无缘
既然无缘 2020-12-11 06:33

I have a component class as EventSchedulePage.It extends HandleStorageService abstract class as shown below.You can see that there is

3条回答
  •  死守一世寂寞
    2020-12-11 07:32

    hmm.. It seems anti-pattern no? I mean using service layer to handle UI related things? That is why I tried to get the solution based on TS.What is your thoughts about this? – Sampath

    It is definitely more MVCS-like (Model-View-Controller-Service) to handle that in the controller. But that is a widely taken approach.

    If you want to go for it, @suraj's answer is my personal recommendation.

    Handling alerts on the controller is certainly possible. Keep reading.

    event-schedule-page.service.ts

    export class EventSchedulePage extends HandleStorageService {
      // ...
      foo() {
        if (!bar) {
          throw new Error('Something went wrong.');
        }
        // ...
      }
    }
    

    home.controller.ts

    export class HomeController {
      // ...
      foo() {
        try {
          eventSchedulePageService.foo();
        } catch (error) {
          window.alert(error); // Handle and UI display the error on the controller.
        }
      }
    }
    

    To follow up, you can use custom error classes or separate functions to throw / handle your errors.

提交回复
热议问题