简体   繁体   中英

Vue.js: How to call method from another exported constent

I am exporting set of constants and using it as mixin. I am not sure how to call methods from different exported constant?

mixins.js

export const exConstOne = {
  methods: {
    fnOne () {
      console.log('exConstOne > fnOne got fired')
    }
  }
}

export const exConstTwo = {
  methods: {
    fnTwo () {
      exConstOne.fnOne()
    }
  },
  mounted () {
    this.fnTwo()
  }
}

MyComponent.vue

<script>
  import { exConstTwo } from './mixins'

  export default {
    name: 'MyComponent',
    mixins: [exConstTwo],
  }
</script>

You forgot the "methods" in your function call.

If you change your code for the following it will work

mixin.js

export const exConstOne = {
  methods: {
    fnOne () {
      console.log('exConstOne > fnOne got fired')
    }
  }
}

export const exConstTwo = {
  methods: {
    fnTwo () {
      exConstOne.methods.fnOne()
    }
  },
  mounted () {
    this.fnTwo()
  }
}

Note : The issue is not really Vue related. It's just a javascript mistake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM