在Vue组件中导入助手类

时间:2016-09-19 19:08:03

标签: javascript vue.js vue-component

我想导入一个辅助类而不是内联组件中的逻辑。我收到以下错误:

http://eslint.org/docs/rules/no-unused-vars  'NavbarService' is defined but never used

/services/NavbarService.js

class NavbarService {
  constructor (init) {
    this.init = init;
  }

  static applications () {
    return [
      { name: 'Administration' },
      { name: 'Standard' }
    ];
  }

  static views () {
    return [
      { name: 'Providers', path: '/providers' },
      { name: 'Authorities', path: '/authorities' },
      { name: 'Services', path: '/services' },
      { name: 'Codes', path: '/codes' }
    ];
  }
}

/components/Navbar.vue

import NavbarService from '../services/NavbarService.js';

export default {
  data () {
    return {
      versionIsVisible: false,
      version: '2.0.0',
      applications: NavbarService.applications(),
      views: NavbarService.views()
    };
  },

  methods: {
    showApplications: function () {
      this.applications = NavbarService.applications();
      this.views = [];

      return;
    }
  }
};

1 个答案:

答案 0 :(得分:3)

根据Roy J的建议,我将 /services/NavbarService.js 更改为:

export default {
  applications: function () {
    return [
      { name: 'Administration' },
      { name: 'Standard' }
    ];
  },

  views: function () {
    return [
      { name: 'Providers', path: '/providers' },
      { name: 'Authorities', path: '/authorities' },
      { name: 'Services', path: '/services' },
      { name: 'Codes', path: '/codes' }
    ];
  }
};