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

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

JS crifan 3409浏览 0评论
折腾:
【未解决】vuejs加上属性变量值
期间,去试试代码:
1
2
3
  data: {
    nxmleVisibility: 'hidden'
  },
结果报错:
1
2
`data` property in component must be a function
eslintvue/no-shared-component-data
计算属性和侦听器 — Vue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
改为
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
export default {
  name: 'Dashboard',
  // nxmleVisibility: 'hidden',
  timer: '',
 
 
  data: () => {
    return {
      nxmleVisibility: 'hidden'
    }
  },
 
 
  computed: {
    ...mapGetters([
      'name'
      // 'nxmleVisibility'
    ])
  },
 
 
  created() {
    // this.timer = setInterval(this.updateSwitchStatus, 1000)
  },
至少没有报错。
【总结】
之前用:
1
2
3
  data: {
    nxmleVisibility: 'hidden'
  },
报错:
1
`data` property in component must be a function
改为:
1
2
3
4
5
  data: () => {
    return {
      nxmleVisibility: 'hidden'
    }
  },
即可。

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

发表我的评论
取消评论

表情

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

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