Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] Support console in the template #7939

Closed
wenfangdu opened this issue Mar 22, 2023 · 9 comments
Closed

[FR] Support console in the template #7939

wenfangdu opened this issue Mar 22, 2023 · 9 comments
Labels
✨ feature request New feature or request

Comments

@wenfangdu
Copy link

wenfangdu commented Mar 22, 2023

What problem does this feature solve?

I don't understand, why console can't be used in the template since Vue 2, it makes template debugging much more complicated than it needs to be, Svelte supports console in the template out of the box, can Vue be more user friendly in this regard?

What does the proposed API look like?

<template>
  <!-- just works, no more `console` doesn't exist -->
  <button @click="console.log">Log</button>
</template>

If we want to discourage console usage in the templates, it can be achieved using eslint lint rules, but not usable at all is a complete overkill.

@sxzz
Copy link
Member

sxzz commented Mar 22, 2023

Console API is not a built-in JavaScript API and I think the most important is that it has side effects. It will be executed multiple times when re-rendering, so it's not suitable to use it in <template>.

@wenfangdu
Copy link
Author

@sxzz Does Svelte have the same issue?

It will be executed multiple times when re-rendering

console in the template is meant for debugging purposes, I don't see a huge negative impact if it's triggered on every render.

If the template console use case is legit and frequent, I think it makes sense to allow it in the template, besides we can always disallow them in production code through eslint rules.

@edison1105
Copy link
Member

edison1105 commented Mar 24, 2023

see #3450 (comment)
another PR #6508

@wenfangdu
Copy link
Author

One of the first things we do when we encounter bugs is to console log the errors, also, the first thing in programming that we learn is to print something. It's just ridiculous such a fundamental thing can't used be in the template, we do console log in JSX and in the Svelte template all the time, and it doesn't hurt anybody. If the template is deliberately made so limited, no wonder some JSX fans call it a toy.

@smallnine9
Copy link
Contributor

But I want know does console work in render() function? like
h('div', {onClik: console.log('hi')}, 'hi')

@smallnine9
Copy link
Contributor

But I want know does console work in render() function? like h('div', {onClik: console.log('hi')}, 'hi')

yes it can, but it should be () => console.log('hi'),
so, it's just a have or not have feature for compiler

@wenfangdu
Copy link
Author

Console API is not a built-in JavaScript API and I think the most important is that it has side effects. It will be executed multiple times when re-rendering, so it's not suitable to use it in <template>.

@sxzz If we want to discourage console usage in the templates, it can be achieved using eslint lint rules, maybe even turned on by default, but not usable at all is an overkill.

@yyx990803
Copy link
Member

The template global whitelist has always only included language-level globals (as defined in ECMA-262). When Vue was initially created, console was also not standardized in anyway, and isn't even guaranteed to be present in some environments. This is the reason why console was not included by default.

However, there is now an actual spec for it, and it seems all major JS runtimes do implement console the same way nowadays, so we can consider adding it as a special case for the concrete DX improvement.

@wenfangdu
Copy link
Author

@yyx990803 Evan, can we add console support in the template in v3.3? Here's the PR.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
✨ feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants