vue中前端怎么读取txt文本文档?

在vue项目中,怎么读取自定义的txt文本文档?

第一步:把要读取的文本文档放到,public文件目录下。

文档内容:

第二步:  定义函数

    inquire(){
     let xhr = new XMLHttpRequest()
     let okStatus = document.location.protocol === "file:" ? 0 : 200;
     xhr.open("GET", "text.txt", false); // public文件夹下的绝对路径
     xhr.overrideMimeType("text/html;charset=utf-8")
     xhr.send(null)
     console.log(xhr.responseText)  // xhr.responseText为文本中的内容
    },

第三步:  调用函数,输入文本中的内容

猜你喜欢

转载自blog.csdn.net/weixin_48674314/article/details/125235360