簡體   English   中英

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

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

我正在嘗試將數組導入Vue組件:

(簡化的)組件:

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

    export default {
        props: [],

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

country_codes.js

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

組件和country_codes.js文件都在同一目錄中,但是countryCodes屬性始終是一個空對象。

我究竟做錯了什么?

我不能說出您的代碼無法正常工作的原因。 也許您只需要從countryCodes.js導出數據

所以,我相信這會起作用

import countryCodes from './countryCodes.js'

countryCodes.js

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM