首页 > 解决方案 > 如何从打字稿上的txt文件中获取数据?

问题描述

在此处输入图像描述

showFile() {
    var fs = require('fs');
    var obj = fs.readFile('dic.txt', 'utf8');
    console.log(obj);
  }

  getItems(ev) {
    // Reset items back to all of the items
    this.initializeItems();

    // set val to the value of the ev target
    var val = ev.target.value;

    // if the value is an empty string don't filter the items
    if (val && val.trim() != '') {
      this.items = this.items.filter((item) => {
        this.showFile();
        return (item.toLowerCase().indexOf(val.toLowerCase()) > -1);
      })
    }
  }

我想制作一个移动应用程序作为字典。我有一个文本文件,其中存储了所有俄语单词和英语翻译。我需要从这个文件中进行搜索,何时输入单词并需要显示翻译。我正在 Ionic 3 上编写代码。我有错误:fs.readFile 不是函数。(在 'fs.readFile('dic.txt','utf8')','fs.readFile' 未定义)

标签: javascripttypescriptionic3es6-modules

解决方案


你可以读取一个json文件

import { HttpClient } from '@angular/common/http'; 
import { Observable } from 'rxjs/Observable';

   constructor(private http: HttpClient) {
        public getTranslation(): Observable<any> {
            return this.http.get("./assets/translation.json")
        }
   }

推荐阅读