link137 link138 link139 link140 link141 link142 link143 link144 link145 link146 link147 link148 link149 link150 link151 link152 link153 link154 link155 link156 link157 link158 link159 link160 link161 link162 link163 link164 link165 link166 link167 link168 link169 link170 link171 link172 link173 link174 link175 link176 link177 link178 link179 link180 link181 link182 link183 link184 link185 link186 link187 link188 link189 link190 link191 link192 link193 link194 link195 link196 link197 link198 link199 link200 link201 link202 link203 link204 link205 link206 link207 link208 link209 link210 link211 link212 link213 link214 link215 link216 link217 link218 link219 link220 link221 link222 link223 link224 link225 link226 link227 link228 link229 link230 link231 link232 link233 link234 link235 link236 link237 link238 link239 link240 link241 link242 link243 link244 link245 link246 link247 link248 link249 link250 link251 link252 link253 link254 link255 link256 link257 link258 link259 link260 link261 link262 link263 link264 link265 link266 link267 link268 link269 link270 link271 link272 link273

[Vue.js] Angular right way to show loading for different components at the same time?

In angular 7+ web-app there is a page, on this page, there is 3 different components, I make 3 different HTTP requests in ngOnInit() to calculate components inputs. and while when making requests to show loading spinners for each component.

My current solution is, there is some other component called loading-component, this loading component takes 1 boolean input variable, and if loading is true it overlays loading spinner on components inside it. For my page where there is 3 different components, HTML looks like this:

<loading-component [loading]=”loadingcomponent1”>
<component1 [input1]=”input1”></component1>
<loading-component [loading]=”loadingcomponent2”>
<component2 [input2]=”input2”></component2>
<loading-component [loading]=”loadingcomponent3”>
<component3 [input3]=”input3”></component3>


loading1: false;
loading2: false;
loading3: false;
input1: null
input2: null
input3: null

ngOnInit() {
this.loading1 = true;
this.http.get(url1).subscribe(resp => {
this.input1 = resp
this.loading1 = false

this.loading2 = true;
this.http.get(url2).subscribe(resp => {
this.input2 = resp
this.loading2 = false

this.loading3 = true;
this.http.get(url3).subscribe(resp => {
this.input3 = resp
this.loading3 = false

I compute 3 different boolean variables for 3 different loading-component. Set true before HTTP request, set false after the response.

My question: Is it a good way or is there any other way (better) to handle this?

ps. In my previous project, which was made with vue.js I used the same strategy

Solution :

I don’t think that it would be that bad. After all you need to keep a flag and a spinner for each component in the case. However it can still be considered as a DRY violation that could easily be fixed with the power of directives.
You can easily create a structural directive that places the spinner depending on the boolean binding you provide.