简体   繁体   中英

Angular: How do I read a text file located in the assets folder?

I am trying to open a.txt file which is in my assets folder. The name is "Maze1.txt". I am sort of new to these things so I was wondering if someone can help me read the text from this file. Thank you so much. If there is any more information needed, please tell me. :)

Nevermind. I found a working solution by using a constructor. For anyone looking for code that works, use this:

private httpClient: HttpClient;

  constructor(http: HttpClient) {
    this.httpClient = http;
  }

and in the function: I used this:

this.httpClient.get('assets/Maze1.txt', { responseType: 'text' })
      .subscribe(data => console.log(data));

You can use the HttpClient for this.

this.httpClient.get('assets/Maze1.txt', {responseType: 'text'})
        .subscribe(data => console.log(data));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM