Categories
Vue Answers

How to fix Vue v-on:click does not work on component?

Sometimes, we want to fix Vue v-on:click does not work on component.

In this article, we’ll look at how to fix Vue v-on:click does not work on component.

How to fix Vue v-on:click does not work on component?

To fix Vue v-on:click does not work on component, we add the native modifier.

For instance, we write

<template>
  <div id="app">
    <test @click.native="testFunction"></test>
  </div>
</template>

to add a native click handler to the root element of the test component by using the @click.native directive.

We set it to testFunction so when the root element of test is clicked, testFunction is called.

Conclusion

To fix Vue v-on:click does not work on component, we add the native modifier.

Categories
Uncategorized Vue Answers

How to watch store values from Vuex with Vue.js?

Sometimes, we want to watch store values from Vuex with Vue.js.

In this article, we’ll look at how to watch store values from Vuex with Vue.js.

How to watch store values from Vuex with Vue.js?

To watch store values from Vuex with Vue.js, we get the value from a getter.

For instance, we write

<script>
import { mapGetters } from "vuex";

export default {
  computed: {
    ...mapGetters(["myState"]),
  },
  watch: {
    myState(val, oldVal) {
      console.log(val, oldVal);
    },
  },
};
</script>

to call mapGetters to map the myState getter to the myState computed property.

Then we add a watcher for myState and get the current value with val and the previous value with oldVal.

Conclusion

To watch store values from Vuex with Vue.js, we get the value from a getter.

Categories
JavaScript Answers

How to stop CKEditor from adding unwanted characters with JavaScript?

To stop CKEditor from adding unwanted characters with JavaScript, w ecan set a few settings.

For instance, we write

CKEDITOR.editorConfig = (config) => {
  config.enterMode = CKEDITOR.ENTER_BR;
  config.entities = false;
  config.basicEntities = false;
};

to set the editorConfig to a function that sets the editor config.

We set enterMode to CKEDITOR.ENTER_BR to convert empty p elements to br elements.

And we set entities and basicEntities to stop characters from being escaped.

Categories
JavaScript Answers

How to make a put request with simple string as request body with JavaScript?

To make a put request with simple string as request body with JavaScript, we set the Content-Type header.

For instance, we write

const response = await axios.put("http://localhost:4000/api/token", "myToken", {
  headers: { "Content-Type": "text/plain" },
});

to call axios.put to make a put request.

The first argument is the request URL, the 2nd argument is the request body, and the last argument is an object with the request headers.

We set the Content-Type header to text/plain to let us send a string request body.

Categories
JavaScript Answers

How to get first element of a collection that matches iterator function with JavaScript?

To get first element of a collection that matches iterator function with JavaScript, we use the array find method.

For instance, we write

const array = [5, 12, 8, 130, 44];
const found = array.find((element) => element > 10);
console.log(found);

to call array.find with a function that returns the first element in array that’s bigger than 10.

ASs a result, found is 12.