Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  
  <div id="app">
    <h2>透過 emit 向外傳遞資訊</h2>
    我透過元件儲值了 {{ cash }} 元
    <button-counter v-on:increment="incrementTotal"></button-counter>
    <hr>
    <button-counter v-on:increment="incrementTotal"></button-counter>
  </div>
  
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.js"></script>
</body>
</html>
 
Vue.component('buttonCounter', {
  template: `<div>
    <button @click="incrementCounter" >增加 {{ counter }} 元</button>
    <input type="number" v-model="counter">
  </div>`,
  data: function() {
    return {
      counter: 1
    }
  },
  methods: {
    incrementCounter: function () {
      this.$emit('increment', Number(this.counter));
    }
  }
});
var app = new Vue({
  el: '#app',
  data: {
    cash: 300
  },
  methods: {
    incrementTotal: function(newCount) {
      console.log(newCount)
      this.cash = this.cash + newCount;
    }
  }
});
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers