Why is the function called in the instance created by Vue: not prefixed with methods??

//html
<div id="app">
  

{{ count }}

<button @click="increment">+</button> <button @click="decrement">-</button>

</div>
//js
// make sure to call Vue.use(Vuex) if using a module system

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
      increment: state => state.countPP,
    decrement: state => state.count--
  }
})

new Vue({
  el: "-sharpapp",
  computed: {
    count () {
        return store.state.count
    }
  },
  methods: {
    increment () {
      store.commit("increment")
    },
    decrement () {
        store.commit("decrement")
    }
  }
})

in the above code,

<button @click="increment">+</button>
<button @click="decrement">-</button>

Why not methods.increment ()?

Mar.22,2021

because vue does its own processing, it proxies the properties defined in methods to the instance. vue Source code:

clipboard.png

vm is the current instance.

The method in

vue is written in methods. You can write the method name directly when you call it. It is injected internally, for example, you define a string arr, in data and write arr directly instead of methods and data. in data.arr


vue when you write it in the template. These are directly exposed to the vue constructor. When you instantiate, their this points to the object you instantiated, and you directly use this.. Method can be called directly

Menu