简体   繁体   English

Vue.js:无法从函数/方法访问数据

[英]Vue.js: Cannot access data from function / methods

I am getting value as undefined when I try to access this.perMonth from fnTwo() and fnThree() but works in fnOne() . 当我尝试从fnTwo()fnThree()访问this.perMonth但在fnOne()工作时,我得到的值undefined I can run a function from data(){} and can return some values but cannot return that's in data(){} eg. 我可以从data(){}运行一个函数,可以返回一些值,但是不能返回data(){}那个值。 this.perMonth (check fnThree() ) this.perMonth (检查fnThree()

 Vue.component('BuySubscription', { template: '#buy-subscription', data() { return { perMonth: 19, valFromFnTwo: this.fnTwo(), valFromFnThree: this.fnThree() } }, methods: { fnOne() { console.log("from fnOne: get data > perMonth: " + this.perMonth); return this.perMonth }, fnTwo() { console.log("from fnTwo: get data > perMonth : " + this.perMonth); return this.perMonth }, fnThree() { console.log("from fnThree: get data > perMonth " + this.perMonth); console.log("from fnThree: get data > valFromFnTwo: " + this.valFromFnTwo); return 123 // retruns static value } } }); new Vue({ el: '#app', }); 
 body { font-family: arial; font-size: 12px} p {margin: 0} 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script> <div id="app" class="col-lg-6 d-flex align-items-center"> <buy-subscription></buy-subscription> </div> <script type="text/x-template" id="buy-subscription"> <div> <p>value from data > perMonth: {{perMonth}}</p> <p>value from data > valFromFnTwo: {{valFromFnTwo}} <span style="color: red"> <-- getting Undefined here (see console)</span></p> <p>value from fnOne(): {{fnOne()}}</p> <p>value from fnTwo(): {{fnTwo()}}</p> <p>value from fnThree(): {{fnThree()}}</p> </div> </script> 

Also, please consider if I have nested array of data which I like to process: 另外,请考虑我是否有要处理的嵌套数据数组:

  data() {
    return {
      perMonth: 19,
      someVarViaFns: [
        {
          valFromFnTwo: this.fnTwo(1),
          valFromFnThree: this.fnThree(2) 
        },        
        {
          valFromFnTwo: this.fnTwo(5),
          valFromFnThree: this.fnThree(9) 
        },
      ]
    }
  }

Calling the Vue instance's methods from within the data method is problematic because the data properties have not been set yet. data方法中调用Vue实例的方法存在问题,因为尚未设置数据属性。 So, any references to data properties in those methods ( this.perMonth in your case) will return undefined . 因此,这些方法(在您的情况下为this.perMonth中对数据属性的任何引用都将返回undefined

Set the values of valFromFnTwo and valFromFnThree in the created or mounted hook instead. valFromFnThreecreatedmounted挂钩中设置valFromFnTwovalFromFnThree的值。 These hooks fire after the data method has returned, so references to data properties will work as expected. 这些钩子在data方法返回后触发,因此对数据属性的引用将按预期工作。

data() {
  return {
    perMonth: 19,
    valFromFnTwo: null,
    valFromFnThree: null
  }
},
mounted() {
  this.valFromFnTwo = this.fnTwo();
  this.valFromFnThree = this.fnThree();
}

I think you're having this issue because of the Hoisting behavior of the JS Engine. 由于JS引擎的提升行为,我认为您遇到了这个问题。

Instead of declaring it in your data use computed properties: 与其在数据中声明它,不如使用计算属性:

computed: {
  fnTwo() {
    // you can do other operations here also
    // the currency variables is just an example. not mandatory
    let currency = 'usd';

    return "value from fnTwo: " + this.perMonth + ' ' + currency;
  }
}

Then you can render it in your template <p>{{ fnTwo }}</p> or even <p>{{ fnTwo()</p> both should work. 然后可以在模板<p>{{ fnTwo }}</p>渲染它,甚至<p>{{ fnTwo()</p>都应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM