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

Templates do not use toString() on objects #3944

Closed
chemicalcrux opened this issue Jun 11, 2021 · 4 comments · Fixed by #4210
Closed

Templates do not use toString() on objects #3944

chemicalcrux opened this issue Jun 11, 2021 · 4 comments · Fixed by #4210
Labels
has workaround A workaround has been found to avoid the problem need guidance The approach/solution in the PR is unclear and requires guidance from maintainer to proceed further.

Comments

@chemicalcrux
Copy link

Version

3.1.1

Reproduction link

https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHRlbXBsYXRlPlxuICA8aDE+e3sgbXNnIH19PC9oMT5cbiAgPGgxPnt7IG90aGVyTXNnIH19PC9oMT5cbiAgPGgxPnt7IG1zZy50b1N0cmluZygpIH19PC9oMT5cbiAgPGgxPnt7IG90aGVyTXNnLnRvU3RyaW5nKCkgfX08L2gxPlxuPC90ZW1wbGF0ZT5cblxuPHNjcmlwdCBzZXR1cD5cbmNvbnN0IG1zZyA9IHtcbiAgZm9vOiBcImJhclwiXG59XG5tc2cudG9TdHJpbmcgPSAoKSA9PiBcIkhlbGxvXCJcbiAgXG5jbGFzcyBUZXN0IHtcbiAgY29uc3RydWN0b3IgKCkge1xuICAgIHRoaXMuYmF6ID0gXCJidXpcIlxuICB9XG4gIHRvU3RyaW5nICgpIHtcbiAgICByZXR1cm4gXCJXb3JsZFwiXG4gIH1cbn1cbiAgXG5jb25zdCBvdGhlck1zZyA9IG5ldyBUZXN0KClcbjwvc2NyaXB0PiJ9

Steps to reproduce

  1. Render an object in a template that has a non-default toString() method

What is expected?

In Vue 2, the template would render with the result of calling toString() on the object.

It would check if the object's toString property was different from Object.toString.

https://github.com/vuejs/vue/blob/dev/src/shared/util.js#L81

What is actually happening?

In Vue 3.1.1, the template renders with the result of running JSON.stringify on the object.

It only checks that it's working with an object.

https://github.com/vuejs/vue-next/blob/master/packages/shared/src/toDisplayString.ts


This feels like a breaking change, but I don't see it listed anywhere. Confusingly, however, I've also seen zero people talking about this being a problem!

Obviously, I can just call .toString() everywhere that I was relying on this behavior, but that's a bit tedious.

@LinusBorg
Copy link
Member

LinusBorg commented Jun 11, 2021

Confusingly, however, I've also seen zero people talking about this being a problem!

I personally only every "dumped" objects in interpolations for debugging. I've never seen toString() being used in that way anywhere. I know it exists, but you are kind of the first person I learn of that relies on this behavior.

That may be just me, of course, but to me it seems this is kind of a niche thing to do, which might explain why it's not being talked about.

That being said, I would think it makes sense to implement the Vue 2 behavior in the sense of a fix. I personally don't think this was an intentional breaking change with a big reason behind it.

@chemicalcrux
Copy link
Author

Yeah, I had a feeling it wasn't very common practice.

I (ab)used the behavior almost exclusively with a language system I'd developed for a game. The vast majority of the usage is in string interpolation, so it's not like too many things broke. It just made for quite a mess when I first got the project to compile, haha :p

@posva posva added has workaround A workaround has been found to avoid the problem need guidance The approach/solution in the PR is unclear and requires guidance from maintainer to proceed further. labels Jun 14, 2021
@posva
Copy link
Member

posva commented Jun 14, 2021

As a workaround, you can use a regular function.

The relevant commit in Vue 2 was vuejs/vue@0e4e45e

catrope added a commit to catrope/vue-next that referenced this issue Jul 29, 2021
Vue 2 did this, but this functionality was quietly removed
from Vue 3 as a side effect of 3c60d40.

Closes vuejs#3944.
@catrope
Copy link
Contributor

catrope commented Jul 29, 2021

The fix from Vue 2 was retained in Vue 3 at first, and was also proposed to be retained in #1100, but was (accidentally?) removed in 3c60d40. I've submitted a pull request to put the old behavior back

@github-actions github-actions bot locked and limited conversation to collaborators Oct 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has workaround A workaround has been found to avoid the problem need guidance The approach/solution in the PR is unclear and requires guidance from maintainer to proceed further.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants