How enable console log in VUE-CLI during development

ぐ巨炮叔叔 提交于 2020-01-16 08:48:08

问题


Hello there community.

I have been trying to figure out how to console.log('whatever') during learning some VueJs development in my methods in order to understand some behaviour of whatever I am doing here.

I understand that there are some questions already asked here and I have scoped into eslint documentation to try an figure this out... I just cannot actually understand what should I do.

This is my methods:

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

This is the error on 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.

So... I have looked into this website: https://eslint.org/docs/rules/no-console

I tried commenting the previous line before console.log... with /*eslint no-console: "error"*/ but it does not work as well.

Dear sir, once reaching out to me on this one, could you please guide me on a step by step in case I need to mess with JSON rules, as I have never done this as well?

I am using vue-cli on webstorm.

Thanks in advance!


回答1:


Edit package.json and in eslintConfig property add

"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

Now, if you want console.log stripped from production build

Edit vue.config.js

and add

// 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
}


来源:https://stackoverflow.com/questions/59366773/how-enable-console-log-in-vue-cli-during-development

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!