当前位置: 移动技术网 > IT编程>网页制作>CSS > vue2.0的使用教程

vue2.0的使用教程

2018年11月09日  | 移动技术网IT编程  | 我要评论

为什么希拉里讨厌中国,冰雨伴奏,光能使者中文版

    vue2.0 #$emit,$on的使用

    2018年10月09日 10:05:54 wandoumm 数:4 个人分类: vue vue1.0中 vm.$dispatch 和 vm.$broadcast 被弃用,改用$emit,$on

    vm.$on( event, callback )

    监听当前实例上的自定义事件。事件可以由vm.$emit触发。回调函数会接收所有传入事件触发函数的额外参数。

    vm.$emit( event, […args] )

    触发当前实例上的事件。附加参数都会传给监听器回调。

    例子:

    //父组件
    <script>
        data () {
          return {
            selecttype: 0,
        },
        methods: {
          onselecttype (type) {
            this.selecttype = type
          }
        }
    </script>

    父组件使用@select-type="onselecttype"监听由子组件vm.$emit触发的事件,通过onselecttype()接受从子组件传递过来的数据,通知父组件数据改变了。

    // 子组件
    

     

    <script> data () { return { selecttype: 0, }, methods: { select (type, event) { this.selecttype = type this.$emit('select-type', type) } } </script>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网