Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a vue component with

 <form @keydown="console.error($event.target.name);"> 

gives

app.js:47961 [Vue warn]: Property or method "console" is not defined on the instance but referenced during render.

window.console doesn't work either

What is the proper way to use console and window in a template to debug?

like image 511
Toskan Avatar asked Jun 28 '18 10:06

Toskan


People also ask

Should I use console error or console log?

console. error is used to output error messages while console. log is the most commonly used console method and is used to log out all sorts of objects or messages. In case of some error browser automatically output the relevant error message while you have to manually log out objects using console.

How do I debug a Vue error?

Setting up a breakpointGo to the debug view in your editor and select your preferred browser configuration. Press 5 or click the green play button. At this point, your breakpoints should hit when the code executes. If you look at the debug view, you can spot some useful data for debugging your application.

How do I comment in a Vue template?

To comment out code in HTML templates you can use HTML comment tag <--! Your comment --> . HTML comments will only be visible to the developer, they won't appear in the page source code.


2 Answers

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)   } }  
like image 196
Un1 Avatar answered Oct 14 '22 03:10

Un1


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.

like image 28
tao Avatar answered Oct 14 '22 04:10

tao