R
R
reasonov2021-11-02 16:04:35
Vue.js
reasonov, 2021-11-02 16:04:35

Is it possible to declare methods and so on by condition?

Hello, I have a question, is it possible to declare a method and so on by condition?
I wrote a slider, for a PC and a sensor, there are touch event handlers and methods for them, which are not used on the PC version. Is it possible to do something like

if(mobile){
  touchStart() ...
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2021-11-02
@Aetae

What problems?

created() {
  if(mobile){
    this.touchStart = () => {}
  }
}

But since mobile or non-mobile is defined once in the life of the application, it is more logical to move the definition of functions / methods to a separate file, like:
let touchStart;
if(mobile){
  touchStart = function() {}
} else {
  touchStart = function() {}
}

export {
  touchStart
}
import {touchStart, ...} from './helpers';
methods: {
  touchStart,
  ...
}

Or as a mixin:
const touchMixin = {
  methods: mobile ? {
    touchStart() {}
  } : {
    touchStart() {}
  }
}
mixins: [touchMixin],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question