How to read txt text document in vue front-end?

How to read a custom txt text document in a vue project?

Step 1: Place the text document to be read in the public file directory.

Document content:

 

Step 2: Define the function

    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为文本中的内容
    },

Step 3: Call the function and enter the content in the text

 

Guess you like

Origin blog.csdn.net/weixin_48674314/article/details/125235360