Nuxt.js 导入普通自定义js文件引发错误Nuxtjs

Nuxt.js 导入普通自定义js文件引发错误Nuxtjs,nuxt.js,Nuxt.js,我在numxt应用程序的根目录中有一个constants.js文件。此文件包含使用module.exports.导出的常量变量 在my/pages/index.vue中导入此文件时 从“../constants”导入{feature1,feature2,feature3,news} 它抛出一个错误,如下所示: TypeError: Cannot set property 'feature1' of undefined at Module.eval (constants.js?c4f5:1

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

在my
/pages/index.vue中导入此文件时

从“../constants”导入{feature1,feature2,feature3,news}
它抛出一个错误,如下所示:

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 = [...];
...
import { feature1, feature2, feature3, news } from '../constants';
export default {
 data() {
      return {
        newsList: news,
        isMobile: false,
        featuresList1: feature1,
        featuresList2: feature2,
        featuresList3: feature3,
        showAll: false
      };
    }
}

index.vue:-

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

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


打我的头。请帮忙

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

index.vue中
而不是导入like
。/constants
导入like
~/routeToLocation

我的代码是:

test.js

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

import { feature1 } from '~/test'
export default {
data() {
    return {
      test: feature1,
           }
       }
}
测试的值是“这是一个测试”

PS:我把我的
test.js
文件放在
numxt.config.js


希望它能起作用

我已经更改了导入,就像您建议的
~/constants
一样,它当然位于我的根项目目录中,位于
numxt.config.js
旁边。但它不起作用!