在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ .vue單文件組件通常是如何調(diào)用實(shí)例方法的?

.vue單文件組件通常是如何調(diào)用實(shí)例方法的?

比如vm.$watch之類的實(shí)例方法,我想要調(diào)用的話一般該寫在哪里?

<script>
export default {
  name: 'componentA',
  data () {
      /*****/
  },
  methods: {
    methodA: function () {
      /*****/
    }
  }
};
</script>
回答
編輯回答
互擼娃

在methods、computed 或者 生命周期鉤子函數(shù)里面都可以調(diào)。原則來說,能訪問到this(vm)實(shí)例的地方都可以。

<script>
export default {
  name: 'componentA',
  data () {
      /*****/
      param:'test'
  },
  computed:{
      param2(val){
         // 計(jì)算屬性等
         this.$set('param', val)
         return val + this.param
      }
  },
  created(){
      // 生命周期鉤子函數(shù)里
      this.$watch('param',(n,o) => {})
  },
  methods: {
    methodA: function () {
      /*****/
      // methods里
      this.$watch('param',(n,o) => {})
    }
  }
};
</script>
2017年3月28日 17:18
編輯回答
不舍棄

如果不在點(diǎn)擊的情況,那么直接寫在computed或生命周期里就可以,但是不建議created,因?yàn)椴荒軋?zhí)行dom操作,可以用mounted

2018年6月27日 06:27
編輯回答
厭遇
<script>
export default {
  name: 'componentA',
  data () {
    text: '123',
      /*****/
  },
  watch: {
    text() {
        console.log(text)
    }
  },
  methods: {
    methodA: function () {
      /*****/
    }
  }
};
</script>
2017年3月8日 22:52