269. Using Subjects for Error Handling
Jul 21, 2026 06:03
· 2:39
· English
· Whisper Turbo
· 2 Altaveus
Aquesta transcripció expirarà 7 dies.
Actualització per a l' emmagatzematge permanent →
Només mostrar
0:01
S…
Speaker 1 (269. Using Subjects for Error Handling)
This other way of handling errors could be interesting in cases like
0:05
S…
Speaker 1 (269. Using Subjects for Error Handling)
when you send
0:07
S…
Speaker 1 (269. Using Subjects for Error Handling)
a request and don't subscribe to it in your component.
0:10
S…
Speaker 1 (269. Using Subjects for Error Handling)
It's easy for us to react to an error in the component here,
0:14
S…
Speaker 1 (269. Using Subjects for Error Handling)
if we do return the observable and we subscribe in the component,
0:18
S…
Speaker 1 (269. Using Subjects for Error Handling)
because then we can pass this second argument to subscribe,
0:21
S…
Speaker 1 (269. Using Subjects for Error Handling)
which is this error handling function.
0:23
S…
Speaker 1 (269. Using Subjects for Error Handling)
But what about creating a post?
0:25
S…
Speaker 1 (269. Using Subjects for Error Handling)
There I subscribe in the service.
0:27
S…
Speaker 1 (269. Using Subjects for Error Handling)
Now obviously you could just return the observable there too,
0:30
S…
Speaker 1 (269. Using Subjects for Error Handling)
and simply subscribe in the component instead,
0:33
S…
Speaker 1 (269. Using Subjects for Error Handling)
and that would not be wrong.
0:35
S…
Speaker 1 (269. Using Subjects for Error Handling)
But you can,
0:36
S…
Speaker 1 (269. Using Subjects for Error Handling)
of course, also implement a different pattern of handling that
0:40
S…
Speaker 1 (269. Using Subjects for Error Handling)
error.
0:40
S…
Speaker 1 (269. Using Subjects for Error Handling)
You could use a subject,
0:42
S…
Speaker 1 (269. Using Subjects for Error Handling)
and that is especially useful if you have multiple places in the application
0:46
S…
Speaker 1 (269. Using Subjects for Error Handling)
that might be interested in your error.
0:48
S…
Speaker 2 (269. Using Subjects for Error Handling)
So,
0:48
S…
Speaker 1 (269. Using Subjects for Error Handling)
for example,
0:49
S…
Speaker 1 (269. Using Subjects for Error Handling)
here, you could have your error
0:53
S…
Speaker 1 (269. Using Subjects for Error Handling)
property,
0:55
S…
Speaker 1 (269. Using Subjects for Error Handling)
which is a new subject,
0:57
S…
Speaker 1 (269. Using Subjects for Error Handling)
and subject needs to be imported from rx .js.
1:01
S…
Speaker 1 (269. Using Subjects for Error Handling)
And that will,
1:02
S…
Speaker 1 (269. Using Subjects for Error Handling)
let's say,
1:03
S…
Speaker 1 (269. Using Subjects for Error Handling)
eventually give us a string,
1:04
S…
Speaker 1 (269. Using Subjects for Error Handling)
that error message,
1:05
S…
Speaker 1 (269. Using Subjects for Error Handling)
but of course you can store in there whatever you want.
1:08
S…
Speaker 1 (269. Using Subjects for Error Handling)
And the idea then simply is that here where we subscribe,
1:12
S…
Speaker 1 (269. Using Subjects for Error Handling)
where we get that error,
1:14
S…
Speaker 1 (269. Using Subjects for Error Handling)
we now use that subject.
1:16
S…
Speaker 1 (269. Using Subjects for Error Handling)
So this error we call next and we pass on error
1:20
S…
Speaker 1 (269. Using Subjects for Error Handling)
message.
1:20
S…
Speaker 1 (269. Using Subjects for Error Handling)
And now the remaining step is that we subscribe to that subject
1:24
S…
Speaker 1 (269. Using Subjects for Error Handling)
in all the places we're interested in that error message.
1:27
S…
Speaker 1 (269. Using Subjects for Error Handling)
So here in this example,
1:30
S…
Speaker 1 (269. Using Subjects for Error Handling)
we would subscribe an ngOnInit for example,
1:33
S…
Speaker 2 (269. Using Subjects for Error Handling)
like this.
1:36
S…
Speaker 1 (269. Using Subjects for Error Handling)
And whenever we get our error message here,
1:38
S…
Speaker 1 (269. Using Subjects for Error Handling)
we could set this error equal to the error message.
1:42
S…
Speaker 1 (269. Using Subjects for Error Handling)
Now, of course,
1:43
S…
Speaker 1 (269. Using Subjects for Error Handling)
as you learned, it's a good and recommended practice to unsubscribe if
1:47
S…
Speaker 1 (269. Using Subjects for Error Handling)
you get rid of this component.
1:48
S…
Speaker 1 (269. Using Subjects for Error Handling)
So I'll add a new property error sub,
1:52
S…
Speaker 1 (269. Using Subjects for Error Handling)
which is a subscription.
1:54
S…
Speaker 1 (269. Using Subjects for Error Handling)
And therefore we need to import subscription from RxJS
1:59
S…
Speaker 2 (269. Using Subjects for Error Handling)
like this.
2:01
S…
Speaker 1 (269. Using Subjects for Error Handling)
And now we can store our subscription in that property.
2:09
S…
Speaker 1 (269. Using Subjects for Error Handling)
here in ng on init and now we just need to add on destroy
2:13
S…
Speaker 1 (269. Using Subjects for Error Handling)
here this interface and import that from at angular core as well
2:17
S…
Speaker 1 (269. Using Subjects for Error Handling)
and add ng on destroy therefore maybe here
2:22
S…
Speaker 1 (269. Using Subjects for Error Handling)
at the end of the component and there we now just want to
2:26
S…
Speaker 1 (269. Using Subjects for Error Handling)
unsubscribe so this error sub
2:30
S…
Speaker 1 (269. Using Subjects for Error Handling)
unsubscribe now we use a subject -based strategy for
2:34
S…
Speaker 1 (269. Using Subjects for Error Handling)
forwarding the error and that is fine too
Aquesta transcripció ha estat generada per reconeixement de veu IA (recloctor automàtic). Pot contenir errors PROXY verificar contra l' àudio original per a ús crític. Política de IA
Resum
Cliqueu Summarize per generar un resum de la IA d' aquesta transcripció.
Summaring...
Pregunta a la IA sobre aquest entorn
Pregunta tot el que sigui d'aquesta transcripció, la IA cercarà seccions i respostes rellevants.