javascript - How to change data() under created hook in vue?
高洛峰
高洛峰 2017-05-19 10:34:46
0
1
605

This is what I write in my component:

A navigation bar

<p class="navbar" v-show="showNavbar">


//js
import Bus from '../../common/js/bus'
export default {
data () {
  return {
    showNavbar: true
  }
},
created () {
  Bus.$on('hideNavbar', function (h) {
    if (h === 1) {
      this.showNavbar = true
    } else if (h === 2) {
      this.showNavbar = false
      console.log('false')
    }
  })

I hope to monitor the status of hideNavbar through Bus.$on, but the browser console reports that showNavbar is not defined
Is there something wrong?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
小葫芦

Try using the arrow function. It should be that the current this is not assigned to the current vue example

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template