【发布时间】:2021-08-25 20:58:54
【问题描述】:
我创建了一个 Vue 应用,目前遇到以下问题:
我有一个包含不同单词的给定文件。
我可以根据需要自定义文件格式,但是Markdown 或text file 会更好。
该文件位于App.vue 文件的旁边 --> 在.src 中。
例如,此文件包含以下内容:
Test1
Test2
Test3
Test4
现在我想将这 4 个单词放入一个数组中。
我在App.vue 中的代码如下所示:
<template>
...
</template
<script>
import { computed, ref } from 'vue'
import someStuff from './components/SomeStuff'
...
// here I want to in put the words
const words= [];
// here a random word is selected from this array
// this works so far when I manually enter words to my array
const randomWord = () => words[Math.floor(Math.random() * words.length)];
export default {
components: { SomeStuff },
setup() { ...
这是我迄今为止尝试过的:
var fs = require('fs');
var array = fs.readFileSync('./src/MyDataSet.txt').toString().split("\n");
for(i in words) {
console.log(words[i]);
}
只有我不知道如何将这个 JS sn-p 包含在我的 Vue 组件中。 require() 没有定义,我想我不能把这段代码放在那里。
我不想拥有任何库、插件或其他依赖项。
【问题讨论】:
标签: javascript arrays vue.js file