How does a .vue single-file component usually call an instance method?

where should I write an instance method such as vm.$watch if I want to call it?

<script>
export default {
  name: "componentA",
  data () {
      /*****/
  },
  methods: {
    methodA: function () {
      /*****/
    }
  }
};
</script>
Mar.09,2021

if you are not clicking, you can write it directly in computed or life cycle, but it is not recommended that created, use mounted


<script>
export default {
  name: 'componentA',
  data () {
    text: '123',
      /*****/
  },
  watch: {
    text() {
        console.log(text)
    }
  }
  methods: {
    methodA: function () {
      /*****/
    }
  }
};
</script>

because it cannot perform dom operation.

can be adjusted in methods, computed, or lifecycle hook functions. In principle, you can access the this (vm) instance anywhere.

<script>
export default {
  name: 'componentA',
  data () {
      /*****/
      param:'test'
  },
  computed:{
      param2(val){
         // 
         this.$set('param', val)
         return val + this.param
      }
  },
  created(){
      // 
      this.$watch('param',(n,o) => {})
  },
  methods: {
    methodA: function () {
      /*****/
      // methods
      this.$watch('param',(n,o) => {})
    }
  }
};
</script>
Menu