简体   繁体   中英

How to use a mixins function inside another mixins function?

How do I combine two functions in vue mixins??

Vue.mixin({
    methods: {
      functionOne: () => {
        console.log(1)
      }
      functionTwo: () => {
        this.functionOne()
      }
    },
    mounted () {
      this.functionTwo()
    }
})

//expected: console 1

//actual: console Cannot read property 'functionOne' of undefined

if you use ES6 arrow functions with Vue methods, it changes the context of this . If you phrase it like:

Vue.mixin({
  methods: {
    functionOne () {
      console.log(1)
    },
    functionTwo: function () {
      this.functionOne()
    }
  },
  mounted () {
    this.functionTwo()
  }
})

it works perfectly.

(the functionOne () and functionTwo: function () syntax are both valid, which is why they both appear in the example.)

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