最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular 4 - How to display 3rdpartylicenses in HTML - Stack Overflow

programmeradmin1浏览0评论

In my package.json the build process I use for my live website "build-prod": "ng build --prod --extract-licenses", I use --extract-licenses because I wish to have the licenses displayed in HTML on my website.

How would I go about displaying this .txt file in HTML with Angular?

In my package.json the build process I use for my live website "build-prod": "ng build --prod --extract-licenses", I use --extract-licenses because I wish to have the licenses displayed in HTML on my website.

How would I go about displaying this .txt file in HTML with Angular?

Share Improve this question edited Nov 3, 2017 at 10:41 Timofey Lavrenyuk 4074 silver badges14 bronze badges asked Nov 1, 2017 at 15:53 Daniel TurcichDaniel Turcich 1,8443 gold badges27 silver badges48 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

It should be available at the root of the domain.

Here's a screenshot of my dist folder after running ng build --prod (which also extracts the licenses):

You should be able to access it by following this code: (Oops! I forgot that you also have to install @angular/mon/http and use the new HttpClient)

With HttpClient introduced recently in Angular 4:

import { HttpClient } from '@angular/mon/http';
export class AboutComponent implements OnInit {
    constructor(private http: HttpClient){}
    license: string;
    ngOnInit(){
        this.http.get('/3rdpartylicenses.txt', {responseType: 'text'})
            .subscribe(result => {
                this.license = result;
            })
    }
}

Using the old Http:

import { Http } from '@angular/http';
export class AboutComponent implements OnInit {
    constructor(private http: Http){}
    license: string;
    ngOnInit(){
        this.http.get('/3rdpartylicenses.txt')
            .map(res => res.text())
            .subscribe(result => {
                this.license = result;
            })
    }
}
<pre [innerText]="license"></pre>

EDIT:

You can also pipe the license property to an async pipe from a HttpClient request.

See the code below for more info:

Component:

import { HttpClient } from '@angular/mon/http';
import { Observable } from 'rxjs';
export class AboutComponent {
  licenses: Observable<string>;
  constructor(private http: HttpClient) {
    this.licenses = http.get('/3rdpartylicenses.txt', { responseType: 'text' });
  }
}

Component template:

<pre>
  <code>
    {{ licenses | async }}
  </code>
</pre>

Here's a StackBlitz demo to play around with!

发布评论

评论列表(0)

  1. 暂无评论