搜尋

首頁  >  問答  >  主體

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

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

1

2

3

4

5

6

7

8

9

<code class="js">var Component = Vue.extend({

  methods: function () {

    print: function () {

      console.log('Go')

    }

  }

});

 

Vue.component('component', Component);</code>

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

1

2

3

<code class="html"><component>

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

</component></code>

大家讲道理大家讲道理2909 天前763

全部回覆(2)我來回復

  • 大家讲道理

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

    1

    2

    3

    4

    5

    6

    7

    <code>var Button= Vue.extend({

      methods: function () {

        print: function () {

          this.$dispatch('print','123')

        }

      }

    });</code>

    回覆
    0
  • 伊谢尔伦

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

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

    回覆
    0
  • 取消回覆