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?
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.
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.
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.
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) } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With