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

Could you please support using symbol value for ":key" field(when using v-for doing list rendering)? #7936

Closed
gitby15 opened this issue Mar 31, 2018 · 3 comments

Comments

@gitby15
Copy link

gitby15 commented Mar 31, 2018

What problem does this feature solve?

When I using "v-for" to render an list, every "instance" in the list has an "id",
I would like to use a symbol type to express my "id" , and fill my "id" into ":key".
Every thing is OK until my "id" has duplicate(Example: push same "instance" into list twice by mistake)

In function "checkDuplicateKeys", when Vue detected duplicate key, it will log a warning, the warnning code is :

"warn(
            ("Duplicate keys detected: '"   key   "'. This may cause an update error."),
            vnode.context
          );"

When the file "key" is symbol type, the browser will throw an error because jointing a symbol value with string, and then byebye my application.

I check the document, It didn't tell us that we can use an symbol into key, so I think it is not a bug.
So, could you please support symbol type in list rendering?

What does the proposed API look like?

  • {{item.content}}
<script> import ... export default Vue.extend({ data(){ return { list:[{id:Symbol('id), content:'a'},{id:Symbol('id), content:'b'},{id:Symbol('id), content:'c'},{id:Symbol('id), content:'d'}] } } }) </script>
@posva
Copy link
Member

posva commented Mar 31, 2018

It works fine: http://jsfiddle.net/jgbsjoxs/
Please, next time consider using the forum, the Discord server or StackOverflow for questions first. But feel free to come back and open an issue if it turns out to be a bug 🙂

@posva posva closed this as completed Mar 31, 2018
@mpawelski
Copy link

It was also not clear for me that symbols as keys are supported. Untill I searched github and found it's explicitly supported since v2.5.12.

I think documentation should be updated and say that key can be number | string | symbol. Also console warning [Vue warn]: Avoid using non-primitive value as key, use string/number value instead. should be changed to [Vue warn]: Avoid using non-primitive value as key, use string/number/symbol value instead.

Then it will be more obvious that this is supported, and not just "happens to work by accident".

@posva
Copy link
Member

posva commented Jun 1, 2020

@mpawelski could you open a pull request at vuejs/vuejs.org please if there isn't one already?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants