Answer the question
In order to leave comments, you need to log in
Global visibility of a custom function anywhere in the code?
How, or rather where, to correctly set the function so that it can be called in any line of code, at least in some of the deep components in the computer properties, at least in vuex mutations, at least in the router?
More specifically, we need a search function for the index by the property value in the object. For some reason, the standard findIndex refuses to work.
indexToFind: function(arr,attr,val) {
for(var i = 0; i < arr.length; i += 1) {
if(arr[i][attr] === val) {
return i;
};
};
return -1;
},
Answer the question
In order to leave comments, you need to log in
Create a utils.js file and put similar functions there.
Export like this in utils.js
Then in the file where you want to use
This is the most adequate approach. You can have hundreds of such functions, as the project grows, littering window or vue global is a bad idea for such small functions.
And here is a good example of what can be written in vue global:
main.js
import axios from 'axios';
Vue.prototype.$http = axios;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question