W
W
WebDev2018-06-01 12:57:17
Vue.js
WebDev, 2018-06-01 12:57:17

How to properly move a function into a vue.js module?

Hello, I have an addStatistics method that works in multiple components. In order not to duplicate the code, I decided to move this method to the functions.js file and put it in the root, and import this file into the components and call it.
Here is the functions.js file code

module.exports = {
    addStatistics: function(link, action) {
        ...
        this.$ls.get('key');
        ...
    }

But the method uses $ls, which is added via Vue.use() in the main.js file.
How can I refer to it in my functions.js file?
import vue from 'vue' throws an error,
const vue = require('vue') works but says $ls = undefined.
How to render this method correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pantagruel964, 2018-06-01
@kirill-93

To transfer in function I twist, not?

S
Sergey Zimoglyadov, 2018-06-05
@wppdevelop

Use mixins https://ru.vuejs.org/v2/guide/mixins.html. In your case, this is the most correct and simple solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question