Я пытаюсь написать перехватчик для моего приложения Angular, который показывает предупреждение Bootstrap при возникновении ошибки.Это то, что я сделал до сих пор:
export class HttpErrorInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request)
.pipe(
retry(1),
catchError((error: HttpErrorResponse) => {
let errorMessage = '';
if (error.error instanceof ErrorEvent) {
// client-side error
errorMessage = `Error: ${error.error.message}`;
} else {
// server-side error
errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
}
// this works: window.alert(errorMessage);
// the following DOM manipulation doesn't work
const p = document.createElement('p');
p.innerHTML = 'hello';
document.appendChild(p);
return throwError(errorMessage);
})
);
}
}
Я пытался загрузить dom, но он показывает мне: createFragment of null error.Как я могу добавить элемент предупреждения Bootstrap в конце тела?Это элемент, который я хочу добавить:
<div class="alert alert-warning alert-dismissible fade show" role="alert">
My error message goes here.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>