A
A
Alexey2018-03-18 03:24:02
webpack
Alexey, 2018-03-18 03:24:02

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;
},

That's where to shove it so that there are no problems with the call?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
lavezzi1, 2018-03-18
@Azperin

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;

I
Ivan, 2018-03-18
@LiguidCool

Extend Vue or Window object?

S
Sergey, 2018-03-18
@SaXXuM

Use mixins.
https://ru.vuejs.org/v2/guide/mixins.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question