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

javascript - Method from imported class undefined - Stack Overflow

programmeradmin1浏览0评论

I'm trying to integrate a third party API into my meteor.js app. So the idea is, when on /blog route, the app should call external class' method.

router.js:

import blog from '../imports/scripts/blog';
FlowRouter('/blog', {
  name: 'blog',
  action: function(params) {
    blog.init(); // here I get the error "init is not a function" (it's undefined)
  }
});

blog.js:

export default class Blog {
  constructor(){
    ...
  }

  init() {
    console.log('init blog api');
    ...
  }
}

I'm using the latest meteor (1.4.2.3) and the following npm packages in order to enable ES2015:

"babel-cli": "^6.18.0",
"babel-core": "^6.18.2",
"babel-loader": "^6.2.8",
"babel-preset-es2015": "^6.18.0",
"babel-runtime": "^6.18.0",
"meteor-node-stubs": "^0.2.4",
"webpack": "^1.13.3"

Am I missing anything in my setup that I cannot call blog.init()?

I'm trying to integrate a third party API into my meteor.js app. So the idea is, when on /blog route, the app should call external class' method.

router.js:

import blog from '../imports/scripts/blog';
FlowRouter('/blog', {
  name: 'blog',
  action: function(params) {
    blog.init(); // here I get the error "init is not a function" (it's undefined)
  }
});

blog.js:

export default class Blog {
  constructor(){
    ...
  }

  init() {
    console.log('init blog api');
    ...
  }
}

I'm using the latest meteor (1.4.2.3) and the following npm packages in order to enable ES2015:

"babel-cli": "^6.18.0",
"babel-core": "^6.18.2",
"babel-loader": "^6.2.8",
"babel-preset-es2015": "^6.18.0",
"babel-runtime": "^6.18.0",
"meteor-node-stubs": "^0.2.4",
"webpack": "^1.13.3"

Am I missing anything in my setup that I cannot call blog.init()?

Share Improve this question edited Dec 4, 2016 at 16:29 Uwe Keim 40.8k61 gold badges188 silver badges304 bronze badges asked Dec 4, 2016 at 16:29 afkatjaafkatja 3191 gold badge5 silver badges13 bronze badges 2
  • 4 you need to do var blog = new Blog(); and then you will have init - blog.init() – Yan Mayatskiy Commented Dec 4, 2016 at 16:37
  • 1 You're exporting a class, not an instance of it. You should do import Blog from '../imports/scripts/blog'; and then const blog = new Blog();. – Michał Perłakowski Commented Dec 4, 2016 at 18:50
Add a ment  | 

3 Answers 3

Reset to default 4

So this is answered by Yan Mayatskiy and Gothdo in their ments, thanks for that. In case anyone was looking for the correct answer:

I needed to instantiate the class I imported, like this:

import Blog from 'blog';
const blog = new Blog();
blog.init();

I think you try import class(not class instance). Therefore you can't call methods.

change you blog.js something like:

class Blog {
  constructor(){
    ...
  }

  init() {
    console.log('init blog api');
    ...
  }
}
export default new Blog();

If you want to export a class you need to instantiate it afterwards, unless is a class with some static methods.

Let's assume this is a "helper" class with only static methods.

export class Logger {
    static info(message) {
        console.log(`Info: ${message}`);
    }

    static error(message) {
        console.log(`Error: ${message}`);
    }
}

export default Logger;

So you can import it and use it straight away.

import Logger from "../../../../utils/Logger";
...
Logger.Info("...");
发布评论

评论列表(0)

  1. 暂无评论