Categories
JavaScript Answers

How to fix property ‘catch’ does not exist on type ‘Observable’ with Rxjs?

Sometimes, we want to fix property ‘catch’ does not exist on type ‘Observable’ with Rxjs.

In this article, we’ll look at how to fix property ‘catch’ does not exist on type ‘Observable’ with Rxjs.

How to fix property ‘catch’ does not exist on type ‘Observable’ with Rxjs?

To fix property ‘catch’ does not exist on type ‘Observable’ with Rxjs, we can use the catchError function.

For instance, we write

import { Observable } from "rxjs";
import { catchError } from "rxjs/operators";
//...

this.http.request(method, url, options).pipe(
  catchError((err: HttpErrorResponse) => {
    //...
  })
);

to call request with pipe to pipe the request results.

Then we call catchError in pipe to catch any errors are returned by the observable returned by request.

Conclusion

To fix property ‘catch’ does not exist on type ‘Observable’ with Rxjs, we can use the catchError function.

Categories
JavaScript Answers

How to add a button that refreshes the page on click with JavaScript?

Sometimes, we want to add a button that refreshes the page on click with JavaScript.

In this article, we’ll look at how to add a button that refreshes the page on click with JavaScript.

How to add a button that refreshes the page on click with JavaScript?

To add a button that refreshes the page on click with JavaScript, we add a click listener to the button.

For instance, we write

<button class="refresh-button">Refresh!</button>

to add a button.

Then we write

const refreshButton = document.querySelector(".refresh-button");

const refreshPage = () => {
  location.reload();
};

refreshButton.addEventListener("click", refreshPage);

to select the button with querySelector.

And then we call addEventListener on the button to add refreshPage as the click event listener.

In refreshPage, we call location.reload to reload the page when we click on the button.

Conclusion

To add a button that refreshes the page on click with JavaScript, we add a click listener to the button.

Categories
Vue Answers

How to handle enter key press in Vue.js?

Sometimes, we want to handle enter key press in Vue.js.

In this article, we’ll look at how to handle enter key press in Vue.js.

How to handle enter key press in Vue.js?

To handle enter key press in Vue.js, we use the @keyup directive.

For instance, we write

<div id="app">
  <input @keyup.enter="onEnter" />
  <h1>{{ msg }}</h1>
</div>

to set @keyup.enter to the onEnter method so that onEnter runs when we press enter in the input.

enter is modifier that makes the @keyup directive only listens to enter key presses.

Conclusion

To handle enter key press in Vue.js, we use the @keyup directive.

Categories
JavaScript Answers

How to remove element from array in forEach loop with JavaScript?

Sometimes, we want to remove element from array in forEach loop with JavaScript.

In this article, we’ll look at how to remove element from array in forEach loop with JavaScript.

How to remove element from array in forEach loop with JavaScript?

To remove element from array in forEach loop with JavaScript, we can use the array splice method.

For instance, we write

const review = ["a", "b", "c", "b", "a"];

review.forEach((item, index, arr) => {
  if (item === "a") {
    arr.splice(index, 1);
  }
});

to call review.forEach with a callback that checks if the item we’re iterating through is 'a'.

If it is, then we call arr.splice with index and 1 to remove the review entry at index.

Conclusion

To remove element from array in forEach loop with JavaScript, we can use the array splice method.

Categories
JavaScript Answers

How to fix Bootstrap’s tooltip doesn’t disappear after button click and mouseleave with JavaScript?

Sometimes, we want to fix Bootstrap’s tooltip doesn’t disappear after button click and mouseleave with JavaScript.

In this article, we’ll look at how to fix Bootstrap’s tooltip doesn’t disappear after button click and mouseleave with JavaScript.

How to fix Bootstrap’s tooltip doesn’t disappear after button click and mouseleave with JavaScript?

To fix Bootstrap’s tooltip doesn’t disappear after button click and mouseleave with JavaScript, we can select all the tooltip toggle elements and then create the tooltip with them.

For instance, we write

const tooltipTriggerList = [
  ...document.querySelectorAll('[data-bs-toggle="tooltip"]'),
];
const tooltipList = tooltipTriggerList.map((tooltipTriggerEl) => {
  return new bootstrap.Tooltip(tooltipTriggerEl, {
    trigger: "hover",
  });
});

to select all tooltip toggle elements with querySelectorAll.

And then we call map on them after spreading them to an array with a callback.

In the callback, we return the Tooltip that we create from each element, which is the tooltipTriggerEl.

And we set the trigger action for each tooltip to 'hover' to show the tooltip when we hover over the tooltip toggle element.

Conclusion

To fix Bootstrap’s tooltip doesn’t disappear after button click and mouseleave with JavaScript, we can select all the tooltip toggle elements and then create the tooltip with them.