代码之家  ›  专栏  ›  技术社区  ›  drake035

无法理解的“Uncaught TypeError:无法读取null的属性'removeChild'错误”

  •  0
  • drake035  · 技术社区  · 5 年前

    在我的应用程序中,单击某个按钮会将其标题更改为另一个,再次单击该按钮会将其更改回默认标题。

    单击它时,我收到以下错误消息:

    Uncaught TypeError:无法读取null的属性“removeChild”

    data () {
      return {
        ctaCaptions: [
          'see case studies',
          'hide case studies'
        ],
        ctaCaption: ''
      }
    },
    methods: {
      handleClick () {
        this.ctaCaption = this.ctaCaptions[1]
      }
    }
    

    HTML格式:

    <a
      @click="handleClick"
      v-html="ctaCaption"
    />
    

    关于是什么导致了这个错误,有什么建议吗?

    0 回复  |  直到 5 年前
        1
  •  0
  •   connexo    5 年前
    methods: {
      handleClick () {
        this.ctaCaption = this.ctaCaptions[1]
      }
    }
    

    内部事件处理程序, this event.target (就你而言 HTMLButtonElement

    methods: {
      handleClick () {
        this.ctaCaption = this.ctaCaptions[1]
      }.bind(this)
    }
    

    或者可以使用ES6箭头函数(这些函数保留 在定义时而不是在执行时):

    methods: {
      handleClick: () => {
        this.ctaCaption = this.ctaCaptions[1]
      }
    }
    

    removeChild .