简体   繁体   English

在Vue.js中导入服务

[英]Importing services in Vue.js

I have a utils service I wish to use in my Vue component: 我想在Vue组件中使用utils服务:

  import utils from './utils';

  export default {
    name: 'grid',
    props: {
      'gridInit': { type: Object, require: true },
      'gridDataInit' : { type: Array, required: true }
    },
    data: function() {
      return {
        gridData: [],
      }
    },
    created: function(){
       this.gridData = utils.transformRawData(this.gridDataInit, this.gridInit.check);
    },

  }

However, I receive: 但是,我收到:

[Vue warn]: Error in created hook: "ReferenceError: utils is not defined"

How can I solve the issue? 我该如何解决这个问题? The structuring of my project with utils services is very important to me. 使用utils服务构建项目对我来说非常重要。

This code says that you have declared utils as you imported. 此代码表明您在导入时已声明utils So that was the declaration part. 这就是声明部分。 But what about definition? 但是定义呢? In definition there is main play happening around for which that declared utils is used in code. 在定义中,发生了主要的变化,在代码中使用了声明的utils So I think it creates warning that it is not defined. 因此,我认为它会发出警告,提示它未定义。 You should use it via registering like Vue.use(componentname/pluginname) . 您应该通过注册Vue.use(componentname/pluginname)来使用它。 This all should be done in index.js and then should be used in components 所有这些都应在index.js中完成,然后应在组件中使用

Anyhow hope this clears for you! 无论如何希望这为您清除!

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

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