【问题标题】:Importing normal custom js file throwing error Nuxtjs导入正常的自定义 js 文件抛出错误 Nuxtjs
【发布时间】:2020-02-12 18:58:57
【问题描述】:

我在 Nuxt 应用程序的根目录中有一个 constants.js 文件。此文件包含使用module.exports.<var_name> 导出的常量变量。

当这个文件被导入到我的/pages/index.vue中时

import { feature1, feature2, feature3, news } from '../constants'; 它抛出如下错误:

TypeError: Cannot set property 'feature1' of undefined
    at Module.eval (constants.js?c4f5:1)
    at eval (constants.js:111)
    at Module../constants.js (pages_index.js:283)
    at __webpack_require__ (runtime.js:796)

constants.js :-

module.exports.feature1 = [...];
module.exports.feature2 = [...];
...

index.vue:-

import { feature1, feature2, feature3, news } from '../constants';
export default {
 data() {
      return {
        newsList: news,
        isMobile: false,
        featuresList1: feature1,
        featuresList2: feature2,
        featuresList3: feature3,
        showAll: false
      };
    }
}

注意 - 一旦您构建并部署到生产环境,此错误就不会出现。很奇怪。

打我的头。请帮忙!

【问题讨论】:

    标签: nuxt.js


    【解决方案1】:

    我测试了你的代码,它对我来说很好,但我改变了一件事

    在您的 index.vue 中,而不是像 ../constants 那样导入像 ~/routeToLocation 那样导入

    我的代码是:

    test.js

    module.exports.feature1 = 'this is a test'
    

    index.vue

    import { feature1 } from '~/test'
    export default {
    data() {
        return {
          test: feature1,
               }
           }
    }
    

    test 的值为“这是一个测试”

    PS:我把我的test.js 文件放在nuxt.config.js 旁边

    希望它有效!

    【讨论】:

    • 我已经按照您的建议更改了导入 ~/constants,这当然是在我的根项目目录中,在 nuxt.config.js 旁边。但它没有奏效!
    猜你喜欢
    • 1970-01-01
    • 2021-12-17
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2022-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多