原代码片段:

new Vue({
      el: '#post-demo',
      data: {
        posts: [
          { id: 1, title: 'My journey with Vue' },
          { id: 2, title: 'Blogging with Vue' },
          { id: 3, title: 'Why Vue is so fun' }
        ]
      }
    })

这时会报错Do not use 'new' for side effects

想要解决这个new无法使用的问题,可以使用如下方法:

let vue = new Vue({
      el: '#post-demo',
      data: {
        posts: [
          { id: 1, title: 'My journey with Vue' },
          { id: 2, title: 'Blogging with Vue' },
          { id: 3, title: 'Why Vue is so fun' }
        ]
      }
    })


Vue.use({
  vm
})

或者直接在本段代码上面添加注释/* eslint-disable no-new */

这样就能使用new了~

好像是个坑,实际上是eslint的配置问题。可以去看看eslint是做什么用的,这个错误产生的原因只是因为没有实例化就new了一个对象,这其实在项目的角度来说是不好的,eslint的作用就是找出这样不好的地方提醒你去改进。

Logo

前往低代码交流专区

更多推荐