Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue, what does $ means?

I'm learning Vue.js and I don't understand what does the $ symbols does. Im using Laravel, I mean I'm not using the Vue-CLI. When I go to the Vue documentation, a lot of the documents does not have the $.

For example the Programmatic Navigation section says: router.push({ path: '/posts' }), but when I did it on my code I had to do this.$router.push({ path: '/posts' });

Thanks in advance.

like image 341
Jacobo Avatar asked Apr 08 '19 08:04

Jacobo


People also ask

What is Vue used for?

Vue. js is a progressive framework for JavaScript used to build web interfaces and one-page applications. Not just for web interfaces, Vue. js is also used both for desktop and mobile app development with Electron framework.

What is Colon in Vue?

Using a colon, or v-bind, you tell Vue that the contents of the attribute, or prop, is dynamic. This means it can be a variable, a javascript expression or function. If you wish to pass in a simple string, you don't need to bind or use the colon.

What is $t in VueJS?

Vuex is a state management pattern for vue. js. $t is the injected method from vue. js or Vue.

What is Vue template?

Vue uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying component instance's data. All Vue templates are syntactically valid HTML that can be parsed by spec-compliant browsers and HTML parsers.


Video Answer


3 Answers

In Vue, $ means that you're using a Vue instance property or an Vue instance method.

You can learn more about it on the documentation.

like image 78
Takachi Avatar answered Oct 23 '22 07:10

Takachi


$ to differentiate vue Instance properties from user-defined properties.

like image 42
Derar rababah Avatar answered Oct 23 '22 06:10

Derar rababah


The $ symbol is used in Vue as a prefix for property names on the Vue instance. This helps to avoid Vue instance properties injected into the Vue prototype by developers from overriding present properties. In essence, it differentiates Vue instance properties from the ones you or other library developers might inject into the Vue instance.

For example. To access the data that the Vue instance is observing you could use: vm.$data. Assuming you assigned your Vue instance to a variable called vm.

An alternative to above, if you are in an SFC(Single File Components), you can access these instances with the this keyword. Like so:

<script>
  export default {
    name: 'mySFCComponentName',
   data() {
     return {
       myData: [1, 2, 3]
    }
   },
  mounted() {
   console.log(this.$data)
  }
 }
</script>

From the above snippet, you can see I am using the $data property on the instance via the this keyword to access the data that the Vue instance is watching.

I hope this helps. Thanks,

like image 42
Kelvin Omereshone Avatar answered Oct 23 '22 06:10

Kelvin Omereshone