为什么不能使用$ emit和$ on?

时间:2017-05-17 12:57:24

标签: javascript vue.js vuejs2

CoffeeScript的:

Vue.component 'levels-list',
    template: '#levels-list-template'
    props: ['uuid', 'type']
    data: ->
        {
            element_levels: []
            other_levels: []
        }
    mounted: ->
        @getElementLevels()
        return
    methods:
        getElementLevels: (uuid) ->
            if typeof uuid == 'undefined'
                uuid = @uuid
            _this = this
            $.get "/ajax/elements/#{uuid}/element_levels.json", (data) ->
                _this.element_levels = []
                _this.other_levels = []
                $.each data.element_levels, (i, element_level) ->
                    _this.element_levels.push(element_level)
                    return
                $.each data.other_levels, (i, other_level) ->
                    _this.other_levels.push(other_level)
                    return
                return
            return

        switchLevel: (uuid) ->
            @getElementLevels(uuid)
            levels.$emit('updateBlocks')
            return

Vue.component 'article-content',
    template: '#article-content-template'
    props: ['uuid']
    data: ->
        {
            questions_blocks: []
            video_screens: []
        }
    mounted: ->
        @getBlocks()
        levels.$on 'updateBlocks', ->
            alert 'GOOOOOD'
            return
        return
    methods:
        getBlocks: ->
            _this = this
            $.get "/ajax/elements/#{@uuid}/load_blocks.json", (data) ->
                _this.questions_blocks = []
                _this.video_screens = []
                $.each data.questions_blocks, (i, questions_block) ->
                    _this.questions_blocks.push(questions_block)
                    return
                $.each data.video_screens, (i, video_screen) ->
                    _this.video_screens.push(video_screen)
                    return
                return
            return

levels = new Vue
    el: '#js-levels'

HTML:

#js-levels
    levels-list uuid="#{@element_level.uuid}" type="#{@element.my_type}"
    article-content uuid="#{@element_level.uuid}"

此代码显示错误:

  

[Vue警告]:挂钩错误:(找到)

     

TypeError:undefined不是对象(评估'级别。$ on')

为什么会出现此错误?

此外,如果我添加console.log行:

mounted: ->
    @getBlocks()
    console.log levels
    levels.$on 'updateBlocks', ->
        alert 'GOOOOOD'
        return
    return

我会成功收到这些街区。但是console.log levels行会返回它:undefined。有什么问题?

0 个答案:

没有答案