如何在开发过程中在 VUE-CLI 中启用控制台登录

Ali*_*h r 5 javascript post eslint vuejs2 vue-cli-3

我一直试图弄清楚如何console.log('whatever')在我的方法中学习一些 VueJs 开发,以了解我在这里所做的任何事情的一些行为。

我知道这里已经提出了一些问题,并且已经将范围纳入ESLINT文档以尝试解决这个问题......我实际上无法理解我应该做什么。

我的代码:

methods: {
    submitData() {
        this.$http.post('https://vue-testing-8a2de.firebaseio.com/data.json', this.user)
                  .then(response => {
                            console.log(response);
                        }, error => {
                            console.log(error)
                        })
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 ESLINT 上的错误:

Failed to compile.

./src/App.vue
Module Error (from ./node_modules/eslint-loader/index.js):
error: Unexpected console statement (no-console) at src/App.vue:35:22:
  33 | this.$http.post('https://vue-testing-8a2de.firebaseio.com/data.json',this.user)
  34 |           .then(response => {
> 35 |                      console.log(response);
     |                      ^
  36 |                  }, error => {
  37 |                      console.log(error)
  38 |                  })


error: Unexpected console statement (no-console) at src/App.vue:37:22:
  35 |                      console.log(response);
  36 |                  }, error => {
> 37 |                      console.log(error)
     |                      ^
  38 |                  })
  39 |             }
  40 |         }


2 errors found.
Run Code Online (Sandbox Code Playgroud)

我查看了这个网站:

我之前尝试评论上一行console.log

  • /*eslint no-console: "error"*/ (但效果不佳)

如果我需要弄乱 JSON 规则,我需要一个分步指南,因为我以前从未这样做过。

vue-cli在 WebStorm 上使用。

提前致谢!

Jar*_*a X 11

编辑package.json并在 eslintConfig 属性中添加

"eslintConfig": { // don't add this, it's already there
    // there's stuff here
    "rules": { // find the rules property
    // addition starts here
        "no-console": "off"
    // addition ends here
    },
    // and keep what was already here
Run Code Online (Sandbox Code Playgroud)

现在,如果您想从生产版本中删除 console.log

编辑 vue.config.js

并添加

// addition starts here
const TerserPlugin = require('terser-webpack-plugin')
const isProd = process.env.NODE_ENV === 'production'
// addition ends here

module.exports = {
    // addition starts here
    configureWebpack: {
        optimization: {
            minimize: true,
            minimizer: isProd ? [
                new TerserPlugin({
                    terserOptions: {
                        ecma: 6,
                        compress: { drop_console: true },
                        output: { comments: false, beautify: false }
                    }
                })
            ] : []
        }
    },
    // addition ends here
    // and keep what was already here
}
Run Code Online (Sandbox Code Playgroud)