账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue emit事件报错 Cannot read property '0' of undefined"
    58
    0

    vue组件通信,运行总是报错[Vue warn]: Error in event handler for "onballdrop": "TypeError: Cannot read property '0' of undefined"
    学习模块化开发,没有使用vuex和vue-bus,这个地方卡了很久,求大神指教
    main

    new Vue({
      data: {
          eventBus: new Vue()
      },
      router: router.routers,
      components: { App },
      render: rout => rout(App)
    }).$mount('#app');

    carcontrol组件

    <div class="add icon-add_circle" @click='addCart'></div>
    
    methods: {
            addCart (event) {
                if (!event._constructed) {
                    return
                }
                if (!this.food.count) {
                    Vue.set(this.food, 'count', 1);
                } else {
                    this.food.count++;
                }
                this.$root.eventBus.$emit('onballdrop', event.target);
                // console.log(this.$root.eventBus);
            }
    }

    shopcar组件

    <transition-group 
            name='drop' 
            @before-enter='beforeEnter' 
            @enter='enter' 
            @after-enter='afterEnter'>
                <div class='ball' v-for='(ball, ballIdx) in balls' 
                v-show='ball.show' :key='ballIdx'></div>
            </transition-group>
    
    data () {
            return {
                balls: [{
                    show: false
                },{
                    show: false
                },{
                    show: false
                },{
                    show: false
                },{
                    show: false
                }],
                dropBalls: [],
            }
        },
    created() {
            this.$root.eventBus.$on('onballdrop', this.dropped)
        },
        methods: {
            dropped(ev) {
                console.log(ev);
                for(let i = 0; i < this.balls.length; i++) {
                    let ball = this.ball[i];
                    if (!ball.show) {
                        ball.show = true;
                        ball.el = ev;
                    }
                    this.dropBalls.push(ball);
                    return;
                }
            }
    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 共 0 条
    • 轻____°ゝ 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部