Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can be exclude `this` keyword in vue.js application?

Tags:

vue.js

vuejs2

Actually I am following Douglas Crockford jslint .

It give warning when i use this.

[jslint] Unexpected 'this'. (unexpected_a) 

I can not see any solution around for the error . Don't say add this in jslist.options and mark it true.

Is there is any approach without using this?

EDIT ADDED CODE

// some vue component here

   <script>
    export default {
      name: "RefereshPage",
      data() {
        return {
          progressValue: 0
        }
      },
      methods:{
        getRefreshQueue(loader){
          console.log(this.progressValue); // ERROR come here [jslint] Unexpected 'this'. (unexpected_a) 
      }
    }
   }
    </script>

Check out this jsfiddle. How can you avoid using this?

https://jsfiddle.net/himmsharma99/ctj4sm7f/5/

like image 976
Himanshu sharma Avatar asked Aug 30 '18 12:08

Himanshu sharma


People also ask

Why we use this keyword in VUE JS?

The this keyword within Vue gives you easy access to all of your data and functionalities. Wether you want to access a data property, a computed property, a component prop or a function, you can all find them directly on the this keyword.

How do I hide components in Vue?

Conclusion. Vue gives you a bunch of good ways to hide the element on the screen. When using v-if="false" the element isn't rendered at all in the DOM. When using v-show="false" the element is rendered in the DOM, however, Vue applies the inline style display: none that hides the element completely.

Is vue2 deprecated?

Everybody uses Vue 2 in 2021! You won't fall behind, you are not missing out, and your codebase is definitely not outdated. Keep your focus on developing amazing apps and when the Vue 3 ecosystem is ready for the migration we will all know it and we will have time in our turn to upgrade.

Do Vue components need a name?

When registering a component, its ID is required, but Name is not. As Vue's Component Documentation says: Registration also automatically sets the component's name with the given id .


2 Answers

as i already stated in the comments:

using this is an integral part of how vue.js works within a component. you can read more about how it proxies and keeps track of dependencies here: https://vuejs.org/v2/api/#Options-Data

like image 200
phoet Avatar answered Nov 23 '22 08:11

phoet


As others have said, you're better off just disabling your linter or switching to ESLint. But if you insist on a workaround, you could use a mixin and the $mount method on a vue instance to avoid using this altogether ..

    let vm;

    const weaselMixin = {
        methods: {
            getdata() {
                console.log(vm.users.foo.name);
            }
        },
        mounted: function () {
            vm.getdata();
        }
    };

    vm = new Vue({
        mixins: [weaselMixin],
        data: {
            users: {
                foo: {
                    name: "aa"
                }
            }
        }
    });

    vm.$mount('#app');

See the modified JSFiddle

As you can see, this only complicates what should be a fairly simple component. It only goes to show that you shouldn't break the way vue works just to satisfy your linter.

I would suggest you go through this article. Particularly important is this part ..

Vue.js proxies our data and methods to be available in the this context. So by writing this.firstName, we can access the firstName property within the data object. Note that this is required.

like image 43
Husam Ibrahim Avatar answered Nov 23 '22 07:11

Husam Ibrahim