【问题标题】:Importing services in Vue.js在 Vue.js 中导入服务
【发布时间】:2018-03-15 19:04:00
【问题描述】:

我希望在我的 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);
    },

  }

但是,我收到:

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

我该如何解决这个问题?使用 utils 服务构建项目结构对我来说非常重要。

【问题讨论】:

  • 似乎是打包/引用错误,问题出在其他地方,而不是上面显示的代码。你的目录结构是什么样的?

标签: javascript import vue.js vuejs2


【解决方案1】:

此代码表示您在导入时已声明 utils。这就是声明部分。但是定义呢?在定义中,在代码中使用了声明 utils 的主要游戏。所以我认为它会产生未定义的警告。您应该通过像Vue.use(componentname/pluginname) 这样的注册来使用它。这一切都应该在 index.js 中完成,然后应该在组件中使用

无论如何希望这一切都为你解决!

【讨论】:

    猜你喜欢
    • 2021-12-01
    • 2020-04-25
    • 2019-05-06
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 2019-08-03
    • 2020-07-08
    • 2021-01-05
    相关资源
    最近更新 更多