search

Home  >  Q&A  >  body text

javascript - 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>
大家讲道理大家讲道理2902 days ago761

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-10 17:15:52

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

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-10 17:15:52

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

    reply
    0
  • Cancelreply