首页 > vue.js 是否可以在 inline-template 綁上方法或屬性

vue.js 是否可以在 inline-template 綁上方法或屬性

今天剛學習 vue 有一個問題想請教
假設我已經有一個 component 如下

var Component = Vue.extend({
  methods: function () {
    print: function () {
      console.log('Go')
    }
  }
});

Vue.component('component', Component);

可否在 HTML 中直接 bind methods 呢?或者說該怎麼做能夠做到動態放入子元素但能用父元件的方法

<component>
  <button v-on:click="print">Print</button>
</component>

var Button= Vue.extend({
  methods: function () {
    print: function () {
      this.$dispatch('print','123')
    }
  }
});

child.$dispatch让事件传播到父元素 parent可以把事件写在events里面

【热门文章】
【热门文章】