简体   繁体   中英

Ember access variable inside Mixin

import Ember from 'ember';
import myConst from '../utils/constants';

export default Ember.Mixin.create(myConst, {
  getFieldId: function(productCode) {
        console.log(myConst.MY_METHODS.FIELD_ID); //Not able to access this
  }
});

My constants.js looks like

var myConst = {};

myConst.MY_METHODS = {
        FIELD_ID: "fieldId"
};

export default myConst;

I am unable to access myConst inside the mixin. What am I doing wrong ?

I think you're confusing a couple of concepts here. In your example, myConst is a utility module that contains some default values, right? In that case, you don't want to mix it in to your mixin (which is what you're doing with Ember.Mixin.create(myConst, {...}) . You should be doing something like this:

import Ember from 'ember';
import myConst from '<app-name>/utils/constants';

export default Ember.Mixin.create({
  getFieldId: function(productCode) {
    console.log(myConst.MY_METHODS.FIELD_ID);
  }
});

If that doesn't work, it's likely something to do with your import path. I would start debugging by just console.log ing myConst to make sure you have the intended object from your module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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