简体   繁体   English

如何在Vue.js组件中使用外部JS文件中的数组?

[英]How do I use an array from an external JS file in a Vue.js component?

I'm trying to import an array into a Vue component: 我正在尝试将数组导入Vue组件:

The (simplified) component: (简化的)组件:

<script type="text/babel">
    const countryCodes = require('./country_codes.js');

    export default {
        props: [],

        data() {
            return {
                countryCodes: countryCodes
            }
        },
    }
</script>

country_codes.js country_codes.js

(function() {
   return ['DE', 'EN']; // This, of course, is way bigger; simplified for readability
});

Both the component and the country_codes.js files are in the same directory, but the countryCodes property is always an empty object. 组件和country_codes.js文件都在同一目录中,但是countryCodes属性始终是一个空对象。

What am I doing wrong? 我究竟做错了什么?

I can't tell for what reason your code isn't working. 我不能说出您的代码无法正常工作的原因。 Maybe you just need to export your data from countryCodes.js 也许您只需要从countryCodes.js导出数据

So, I am sure this will work 所以,我相信这会起作用

import countryCodes from './countryCodes.js'

countryCodes.js countryCodes.js

export const countryCodes = {
  return ['DE', 'EN'];
}

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

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