how can I use console.error or console.log in a vue template?

后端 未结 5 1860
自闭症患者
自闭症患者 2021-02-05 03:44

I have a vue component with

 

gives

app.js:47961 [Vue war

相关标签:
5条回答
  • 2021-02-05 04:18

    Simplest way of providing global objects to the template is to place them in computed, like this:

    console: () => console. Same goes for window,

    computed: {
      console: () => console,
      window: () => window,
    }
    

    See it here.

    0 讨论(0)
  • 2021-02-05 04:20

    If you want to run it inline instead of using a method, just add this to the form:

    Codepen: https://codepen.io/x84733/pen/PaxKLQ?editors=1011

    <form action="/" @keydown="this.console.log($event.target.name)">
      First: <input type="text" name="fname"><br>
      Second: <input type="text" name="fname2"><br>
    </form>
    

    But it'd be better to use a method instead of running functions inline, so you have more control over it:

    <!-- Don't forget to remove the parenthesis -->
    <form action="/" @keydown="debug">
      First: <input type="text" name="fname"><br>
      Second: <input type="text" name="fname2"><br>
    </form>
    
    ...
    
    methods: {
      debug (event) {
        console.log(event.target.name)
      }
    } 
    
    0 讨论(0)
  • 2021-02-05 04:23

    I'd make a getter for console template variable:

        get console() { return window.console; }
    
    0 讨论(0)
  • 2021-02-05 04:38

    Also if you want to access console from {{ }} you can use global mixin:

    Vue.mixin({
        computed: {
            console: () => console
        }
    })
    
    0 讨论(0)
  • 2021-02-05 04:40

    You can use this.console instead console or wrap call to console in a method, i am using eslint config with rule 'no-console': 'off'

    0 讨论(0)
提交回复
热议问题