简体   繁体   中英

How to include imported modules in an export?

This is the module I'm exporting. Nothing much, just initializing Parse 's JavaScript SDK:

mixin.js:

import Parse from 'parse'

var ParseMixin = {
  created: function () {
    Parse.initialize('APP_ID', 'CLIENT_ID')
  }
}

export default ParseMixin

And this is how I'm importing it and using it:

main.js:

import ParseMixin from '../mixins'

export default {
  mixins: [ParseMixin],

  methods: {
    submit () {
      const project = {
        title: this.title,
        content: this.content
      }

      const Obj = Parse.Object.extend(store.class)
      const obj = new Obj()
      obj.save(project).then(() =>
        console.log('Saved.')
      )

      ...

However, right now I get this error:

error no-undef "Parse" is not defined
/home/alex/node/cotrib/src/components/ProjectList.js:54:19 const Obj = Parse.Object.extend(store.class)

Because Parse is not being imported from mixin.js .

How to modify the code so that Parse is also imported?

If you need to reference Parse in main.js , then you should load it there:

// main.js
import Parse from 'parse'
import ParseMixin from '../mixins'

Every module should import its dependencies.


You could expose Parse from ParseMixin if you wanted to:

// mixins.js
export {Parse};

// main.js
import ParseMixin, {Parse} from '../mixins';

but having default and named exported seems to be more confusing than directly importing the dependency.

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