@@ -1590,14 +1590,141 @@ type: api
1590
1590
1591
1591
- If both event and callback are given, remove the listener for that specific callback only.
1592
1592
1593
- ### vm.$emit( event , [...args] )
1593
+ ### vm.$emit( eventName , [...args] )
1594
1594
1595
1595
- **Arguments:**
1596
- - ` {string} event `
1596
+ - ` {string} eventName `
1597
1597
- ` [... args]`
1598
1598
1599
1599
Trigger an event on the current instance. Any additional arguments will be passed into the listener's callback function.
1600
1600
1601
+ - **Examples:**
1602
+
1603
+ Using ` $emit` with only an event name:
1604
+
1605
+ ` ` ` js
1606
+ Vue .component (' welcome-button' , {
1607
+ template: `
1608
+ <button v-on:click="$emit('welcome')">
1609
+ Click me to be welcomed
1610
+ </button>
1611
+ `
1612
+ })
1613
+ ` ` `
1614
+ ` ` ` html
1615
+ < div id= " emit-example-simple" >
1616
+ < welcome- button v- on: welcome= " sayHi" >< / welcome- button>
1617
+ < / div>
1618
+ ` ` `
1619
+ ` ` ` js
1620
+ new Vue ({
1621
+ el: ' #emit-example-simple' ,
1622
+ methods: {
1623
+ sayHi : function () {
1624
+ alert (' Hi!' )
1625
+ }
1626
+ }
1627
+ })
1628
+ ` ` `
1629
+ {% raw %}
1630
+ <div id="emit-example-simple" class="demo">
1631
+ <welcome-button v-on:welcome="sayHi"></welcome-button>
1632
+ </div>
1633
+ <script>
1634
+ Vue.component('welcome-button', {
1635
+ template: `
1636
+ < button v- on: click= " $emit('welcome')" >
1637
+ Click me to be welcomed
1638
+ < / button>
1639
+ `
1640
+ })
1641
+ new Vue({
1642
+ el: '#emit-example-simple',
1643
+ methods: {
1644
+ sayHi: function () {
1645
+ alert('Hi!')
1646
+ }
1647
+ }
1648
+ })
1649
+ </script>
1650
+ {% endraw %}
1651
+
1652
+ Using ` $emit` with additional arguments:
1653
+
1654
+ ` ` ` js
1655
+ Vue .component (' magic-eight-ball' , {
1656
+ data : function () {
1657
+ return {
1658
+ possibleAdvice: [' Yes' , ' No' , ' Maybe' ]
1659
+ }
1660
+ },
1661
+ methods: {
1662
+ giveAdvice : function () {
1663
+ var randomAdviceIndex = Math .floor (Math .random () * this .possibleAdvice .length )
1664
+ this .advice =
1665
+ this .$emit (' give-advice' , this .possibleAdvice [randomAdviceIndex])
1666
+ }
1667
+ },
1668
+ template: `
1669
+ <button v-on:click="giveAdvice">
1670
+ Click me for advice
1671
+ </button>
1672
+ `
1673
+ })
1674
+ ` ` `
1675
+
1676
+ ` ` ` html
1677
+ < div id= " emit-example-argument" >
1678
+ < magic- eight- ball v- on: give- advice= " showAdvice" >< / magic- eight- ball>
1679
+ < / div>
1680
+ ` ` `
1681
+
1682
+ ` ` ` js
1683
+ new Vue ({
1684
+ el: ' #emit-example-argument' ,
1685
+ methods: {
1686
+ showAdvice : function (advice ) {
1687
+ alert (advice)
1688
+ }
1689
+ }
1690
+ })
1691
+ ` ` `
1692
+
1693
+ {% raw %}
1694
+ <div id="emit-example-argument" class="demo">
1695
+ <magic-eight-ball v-on:give-advice="showAdvice"></magic-eight-ball>
1696
+ </div>
1697
+ <script>
1698
+ Vue.component('magic-eight-ball', {
1699
+ data: function () {
1700
+ return {
1701
+ possibleAdvice: ['Yes', 'No', 'Maybe']
1702
+ }
1703
+ },
1704
+ methods: {
1705
+ giveAdvice: function () {
1706
+ var randomAdviceIndex = Math.floor(Math.random() * this.possibleAdvice.length)
1707
+ this.advice =
1708
+ this.$emit('give-advice', this.possibleAdvice[randomAdviceIndex])
1709
+ }
1710
+ },
1711
+ template: `
1712
+ < button v- on: click= " giveAdvice" >
1713
+ Click me for advice
1714
+ < / button>
1715
+ `
1716
+ })
1717
+ new Vue({
1718
+ el: '#emit-example-argument',
1719
+ methods: {
1720
+ showAdvice: function (advice) {
1721
+ alert(advice)
1722
+ }
1723
+ }
1724
+ })
1725
+ </script>
1726
+ {% endraw %}
1727
+
1601
1728
## Instance Methods / Lifecycle
1602
1729
1603
1730
### vm.$mount( [elementOrSelector] )
0 commit comments