Skip to content

Warn when a data property name starts with $ or _ #7070

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

Closed
ywwhack opened this issue Nov 16, 2017 · 8 comments
Closed

Warn when a data property name starts with $ or _ #7070

ywwhack opened this issue Nov 16, 2017 · 8 comments

Comments

@ywwhack
Copy link

ywwhack commented Nov 16, 2017

Version

2.5.3

Reproduction link

http://jsfiddle.net/p48cLbe3/3/

Steps to reproduce

new Vue({
  el: '#app',
  data: {
    // invalid, not proxy by vm
    $a: 'a'
  },
  computed: {
    // valid
    $b() {
      return 'b'
    }
  },
  methods: {
    // valid
    $c() {
      console.log('c')
    }
  }
})

What is expected?

Property name starts with $ or _ identifier should all valid or invalid whether defined in data/computed/methods option, i think these options's behaviors should be consistent

What is actually happening?

It's valid to define $xxx property in computed/methods option, but fall to define $xxx in data option

@posva
Copy link
Member

posva commented Nov 16, 2017

Just in case, there's more information at https://vuejs.org/v2/api/#data, and it's both, for $ and _

As stated at #5879 (comment), we're open on PRs for warnings. Here's a similar commit that implemented warning for conflicting methods that started with _

Related: #6312

@posva posva changed the title Property name start with $ or _ has different behaviors when defined in data/computed/methods Warn when a computed property name starts with $ or _ Nov 16, 2017
@rpemberton
Copy link

@posva is the new title correct? Do you only want to warn for computed properties? I would have thought the warning would be for data + computed + methods. Or just for data.

Happy to help with this but just need to be sure what needs doing :)

@posva
Copy link
Member

posva commented Nov 19, 2017

On second thought, I'm not sure we should introduce this warning as it would show warnings for many plugins dev/users as they purposely use the $ to mark internals like vuelidate ($v). we could, however, do something similar to the commit I referenced for computed properties.

@chrisvfritz already proposed to add the warning functionality under another config flag to not flood existing plugins using $ (#6312 (comment))

Maybe we could go the other way around, adding an error for conflicting data properties and proxying them as well

@rpemberton
Copy link

I think main issue from the example above is that when a data property begins with $ or _ the warning says that the data property is not defined, which could be confusing.

How about if a data property begins with $ or _ then a console warning is shown which is along the lines of what the docs say:

Properties that start with _ or $ will not be proxied on the Vue instance because they may conflict with Vue’s internal properties and API methods. You will have to access them as vm.$data._property.

@posva
Copy link
Member

posva commented Nov 19, 2017

@rpemberton That's perfectly fine as we're not breaking anything, feel free to do the PR and thank you 🙂

@posva posva changed the title Warn when a computed property name starts with $ or _ Warn when a data property name starts with $ or _ Nov 19, 2017
@coolzjy
Copy link
Contributor

coolzjy commented Nov 20, 2017

@posva
I was a little bit confused by the private property names rule in vue style guide, for a $ or _ prefixed property in data makes no sense. should we rework this rule to provide a better way to define private properties?

@jungleBadger
Copy link

jungleBadger commented Nov 23, 2017

I've been reading this issue in order to understand what I could do to help and if there was nobody working on it already but seems that it is invalided by now and feature can not be implemented without breaking other things, right? Can you guys update this issue and remove the contribution tag?

@posva
Copy link
Member

posva commented Nov 23, 2017

let's close this for the moment, thanks for the reminder 🙂

@posva posva closed this as completed Nov 23, 2017
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

Successfully merging a pull request may close this issue.

5 participants