How does Uniapp read files in the root directory?

In uniapp, you can use the uni.request() method to read root directory files. The specific steps are as follows:

  1. One option would be: rewrite the following text in your own words.
  2. invoke uni.request()
<script>
import uni from 'uni_modules/uni-xxxxx/uni.js';
export default {
  // ...
}
</script>
  1. Call the request method of the uni object.
  2. “file.txt located in the static directory”
uni.request({
  url: '/static/file.txt',
  success: (res) => {
    console.log('读取成功', res)
  },
  fail: (err) => {
    console.log('读取失败', err)
  }
})

In which case, res is the returned data after a successful request and err is the error message for a failed request.

It is important to note that the request URL for the uni.request() method must start with a ‘/’ to indicate a path relative to the root directory. If you need to access files from other directories, you can specify a relative path in the URL.

bannerAds