最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】vuejs中加上data中的变量但报错:data property in component must be a function

JS crifan 3042浏览 0评论
折腾:
【未解决】vuejs加上属性变量值
期间,去试试代码:
  data: {
    nxmleVisibility: 'hidden'
  },
结果报错:
`data` property in component must be a function
eslintvue/no-shared-component-data
计算属性和侦听器 — Vue.js
export default {
  name: 'Dashboard',
  // nxmleVisibility: 'hidden',
  timer: '',


  data: {
    nxmleVisibility: 'hidden'
  },


  computed: {
    ...mapGetters([
      'name'
      // 'nxmleVisibility'
    ])
  },
问题依旧。
vuejs data property in component must be a function
Components Basics — Vue.js
Vue.js: Why Components’ Data Properties Must Be Functions
vue.js – Why must vue component data be a function? – Stack Overflow
改为
export default {
  name: 'Dashboard',
  // nxmleVisibility: 'hidden',
  timer: '',


  data: () => {
    return {
      nxmleVisibility: 'hidden'
    }
  },


  computed: {
    ...mapGetters([
      'name'
      // 'nxmleVisibility'
    ])
  },


  created() {
    // this.timer = setInterval(this.updateSwitchStatus, 1000)
  },
至少没有报错。
【总结】
之前用:
  data: {
    nxmleVisibility: 'hidden'
  },
报错:
`data` property in component must be a function
改为:
  data: () => {
    return {
      nxmleVisibility: 'hidden'
    }
  },
即可。

转载请注明:在路上 » 【已解决】vuejs中加上data中的变量但报错:data property in component must be a function

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.176 seconds, using 22.11MB memory