最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Possible to have a computed property and a method of same name in Vue - Stack Overflow

programmeradmin1浏览0评论

I have a method and puted property of same name, when I try to access these, I am getting following error:

vue.min.js:6 Uncaught TypeError: doubleAge is not a function(…)

Following is the simplified code from jsfiddle:

var demo = new Vue({
    el: '#demo',
    data: function(){
        return {
        age: ''
      };
    },
    puted: {
      doubleAge: function () {
            return 2*this.age
        }
    },
    methods: {
      doubleAge: function () {
         return 'this is from method'
       }
    }   
})

I have doubleAge as puted property and method both. Is there some other way to call the method in this case, how to make this work?

I have a method and puted property of same name, when I try to access these, I am getting following error:

vue.min.js:6 Uncaught TypeError: doubleAge is not a function(…)

Following is the simplified code from jsfiddle:

var demo = new Vue({
    el: '#demo',
    data: function(){
        return {
        age: ''
      };
    },
    puted: {
      doubleAge: function () {
            return 2*this.age
        }
    },
    methods: {
      doubleAge: function () {
         return 'this is from method'
       }
    }   
})

I have doubleAge as puted property and method both. Is there some other way to call the method in this case, how to make this work?

Share Improve this question edited Jul 13, 2022 at 22:56 tony19 139k23 gold badges278 silver badges347 bronze badges asked Dec 16, 2016 at 2:28 SaurabhSaurabh 73.7k44 gold badges191 silver badges251 bronze badges 3
  • An object property can only name one thing. Why don't you give them different names? – Barmar Commented Dec 16, 2016 at 2:37
  • @Barmar I had a case where some places I wanted a variable from puted property and some places I want to get it from backend, may be I can rename it to latestMyVar if it's not possible. – Saurabh Commented Dec 16, 2016 at 2:45
  • I tried using vue.js (unminified version), but it didn't give any more descriptive error in this case. Usually the unminified version provides more info than the production version, which you can consider for jsFiddle and development env. – Mani Jagadeesan Commented Dec 16, 2016 at 2:59
Add a ment  | 

1 Answer 1

Reset to default 8

No It's not possible, every property on an object must be uniquely named

发布评论

评论列表(0)

  1. 暂无评论