如何从我的源中导入 .txt 文件? [英] How to import a .txt file from my source?
本文介绍了如何从我的源中导入 .txt 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试导入 .txt 文件以在文本框中显示文本.
我的代码:
import React, { Component } from 'react';导入'./LoadMyFile.css';从 './sample.txt' 导入 myText;导出默认类 LoadMyFile 扩展组件 {使成为() {返回 (<div><button onClick={this.handleClick} className="LoadMyFile" name="button" variant="flat">测试字符串</button>
)}handleClick = () =>{控制台日志(我的文本);}}
但我在控制台看到:/static/media/sample.f2e86101.txt
这里出了什么问题?
解决方案
我已经解决了我的问题.
handleClick = () =>{fetch('/sample.txt').then((r) => r.text()).then(文本=> {控制台日志(文本);})}
这个链接确实有帮助:从公共文件夹 ReactJS 中获取本地 JSON 文件
I try to import a .txt file to show the text in a text box.
My code:
import React, { Component } from 'react';
import './LoadMyFile.css';
import myText from './sample.txt';
export default class LoadMyFile extends Component {
render() {
return (
<div>
<button onClick={this.handleClick} className="LoadMyFile" name="button" variant="flat">test string</button>
</div>
)
}
handleClick = () => {
console.log(myText);
}
}
But i see in console: /static/media/sample.f2e86101.txt
What is going wrong here?
解决方案
I've solved my problem.
handleClick = () => {
fetch('/sample.txt')
.then((r) => r.text())
.then(text => {
console.log(text);
})
}
Tis link did help: Fetch local JSON file from public folder ReactJS
这篇关于如何从我的源中导入 .txt 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文