Type 'string' is not assignable to type 'Subject<string>'

Viewed 120

I am trying to implement some PDF Viewer ngx-extended-pdf-viewer

The sample code provided by the developer of the custom component to read a Base64 string and load in viewer is like the snippet below. When i use the code below i get the following error

Type 'string' is not assignable to type 'Subject'

Not sure what causes and fix it.

export class Base64Component {
  public base64 = new Subject<string>();

  constructor(private httpClient: HttpClient) {}

  public ngOnInit(): void {
    this.httpClient.get(
        '/assets/pdfs/Bootstrap-vs-Material-Design-vs-Prime-vs-Tailwind.base64.txt',
        { responseType: 'text' as 'json' })
      .pipe(
        tap((base64) => (this.base64. = base64 as string)),
      ).subscribe();
  }

enter image description here

1 Answers

You should return same data type.Try this method:

     base64Subject = new Subject<string>();
        //transform subject to observable
          $base64 = this.base64Subject.asObservable();
        //create a function to change subject value
        newBase64(base64:string){
            this.base64Subject.next(base64);
        }
    //Your ngOnInit function will be like this
      public ngOnInit(): void {
        this.httpClient.get(
            '/assets/pdfs/Bootstrap-vs-Material-Design-vs-Prime-vs-Tailwind.base64.txt',
            { responseType: 'text' as 'json' })
          .pipe(
            tap((base64) => (this.newBase64(base64.Data+""))),
          ).subscribe();
      }

// get base64 data from $base64 when there is a change

    this.$base64.subscribe((base64)=>console.log(base64))
Related