从Angular项目的页面中加载来自资源文件夹的图标时显示429错误。

huangapple go评论61阅读模式
英文:

Loading icons from asset folder in a page in angular project showing 429 error

问题

在我的Angular项目中,在assets文件夹下有一些SVG图标。我需要在每个项目旁边的下拉列表中显示这些图标。以下是显示图标的代码:

constructor(		
    private matIconRegistry: MatIconRegistry,
    private domSanitizer: DomSanitizer		
) {
    this.registerIcon();
}

registerIcon() {
    this.matIconRegistry.addSvgIcon("1", this.domSanitizer.bypassSecurityTrustResourceUrl("assets/icons/text.svg"));
    this.matIconRegistry.addSvgIcon("2", this.domSanitizer.bypassSecurityTrustResourceUrl("assets/icons/text.svg"));
    this.matIconRegistry.addSvgIcon("4", this.domSanitizer.bypassSecurityTrustResourceUrl("assets/icons/select.svg"));
    this.matIconRegistry.addSvgIcon("5", this.domSanitizer.bypassSecurityTrustResourceUrl("assets/icons/radio.svg"));
    this.matIconRegistry.addSvgIcon("6", this.domSanitizer.bypassSecurityTrustResourceUrl("assets/icons/checkbox.svg"));
    this.matIconRegistry.addSvgIcon("7", this.domSanitizer.bypassSecurityTrustResourceUrl("assets/icons/date.svg"));		
}

HTML代码:

<mat-form-field>
    <mat-label>Field Type</mat-label>
    <mat-select [(ngModel)]="field.fieldType" [ngModelOptions]="{standalone: true}">
        <mat-select-trigger>
            <mat-icon svgIcon="{{field.fieldType}}"></mat-icon>
            &nbsp;{{selectedFieldName}}
        </mat-select-trigger>
        <mat-option *ngFor="let item of allFieldTypes" [value]="item.value">
            <mat-icon class="material-icons" svgIcon="{{item.value}}"></mat-icon>
            {{item.viewValue}}
        </mat-option>
    </mat-select>
</mat-form-field>

在本地环境运行项目时,一切正常,我可以在下拉列表中看到图标。但是当我将代码部署到AWS云上时,代码不起作用,我看到每个图标都有429错误。

你可以告诉我如何解决这个问题吗?

英文:

In my angular project, under assets folder I have some svg icons. I need to show those icons in the dropdown list beside of each item. Here is my code to show the icons:

constructor(		
		private matIconRegistry: MatIconRegistry,
		private domSanitizer: DomSanitizer		
	) {
		
		this.registerIcon();
	}
	

registerIcon() {
		this.matIconRegistry.addSvgIcon(&quot;1&quot;, this.domSanitizer.bypassSecurityTrustResourceUrl(&quot;assets/icons/text.svg&quot;));
		this.matIconRegistry.addSvgIcon(&quot;2&quot;, this.domSanitizer.bypassSecurityTrustResourceUrl(&quot;assets/icons/text.svg&quot;));
		this.matIconRegistry.addSvgIcon(&quot;4&quot;, this.domSanitizer.bypassSecurityTrustResourceUrl(&quot;assets/icons/select.svg&quot;));
		this.matIconRegistry.addSvgIcon(&quot;5&quot;, this.domSanitizer.bypassSecurityTrustResourceUrl(&quot;assets/icons/radio.svg&quot;));
		this.matIconRegistry.addSvgIcon(&quot;6&quot;, this.domSanitizer.bypassSecurityTrustResourceUrl(&quot;assets/icons/checkbox.svg&quot;));
		this.matIconRegistry.addSvgIcon(&quot;7&quot;, this.domSanitizer.bypassSecurityTrustResourceUrl(&quot;assets/icons/date.svg&quot;));		
	}	

Html code:

&lt;mat-form-field&gt;
	&lt;mat-label&gt;Field Type&lt;/mat-label&gt;
	&lt;mat-select [(ngModel)]=&quot;field.fieldType&quot; [ngModelOptions]=&quot;{standalone: true}&quot;&gt;
		&lt;mat-select-trigger&gt;
			&lt;mat-icon svgIcon=&quot;{{field.fieldType}}&quot;&gt;&lt;/mat-icon&gt;
			&amp;nbsp;{{selectedFieldName}}
		&lt;/mat-select-trigger&gt;
		&lt;mat-option *ngFor=&quot;let item of allFieldTypes&quot; [value]=&quot;item.value&quot;&gt;
			&lt;mat-icon class=&quot;material-icons&quot; svgIcon=&quot;{{item.value}}&quot;&gt;&lt;/mat-icon&gt;
			{{item.viewValue}}
		&lt;/mat-option&gt;
	&lt;/mat-select&gt;
&lt;/mat-form-field&gt;

While runing the project in my local Environment, it's working fine and I can see the icons in the dropdown list. But when I deploy the code to the AWS Cloud, the code is not working and I can see the 429 error for each icons.

从Angular项目的页面中加载来自资源文件夹的图标时显示429错误。

Can anyone tell me how to fix the problem?

答案1

得分: 1

你的服务配置似乎有问题,
这是一个更新的服务,
这是文件夹结构
文件夹结构

以及输出
输出


import { Injectable } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { MatIconRegistry } from '@angular/material/icon';

@Injectable({
  providedIn: 'root'
})
export class IconService {

  constructor(
    private _iconRegistry: MatIconRegistry,
    private _sanitizer: DomSanitizer
  ) {}
  public registerIcons(icons: Array<string>): void {
    icons.forEach((icon) => {

      this._iconRegistry.addSvgIcon(
        icon,
        this._sanitizer.bypassSecurityTrustResourceUrl(
          `assets/icons/${icon}.svg`
        )
      );
    });
  }
}

对于 app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { IconService } from './services/icon.service';
import { MatIconModule } from '@angular/material/icon';
import { HttpClientModule } from '@angular/common/http';
import { MatSelectModule } from '@angular/material/select';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    MatIconModule,
    BrowserAnimationsModule,
    HttpClientModule,
    FormsModule,
    MatSelectModule,
    ReactiveFormsModule
  ],
  providers: [IconService],
  bootstrap: [AppComponent]
})
export class AppModule { }


对于 app.component.ts

import { Component, OnInit } from '@angular/core';
import { IconService } from './services/icon.service';

export class AppComponent implements OnInit {
  title = 'app-icons';
  icon: string = 'soc-icon';

  allFieldType=[];

  field:any

  constructor(private _iconService: IconService) {
    this.field = {
      value: this.icon,
      viewValue: 'Text',
    };
    this.allFieldTypes = [
      { value: this.icon, viewValue: 'Text' },
      { value: this.icon, viewValue: 'Number' },
      { value: this.icon, viewValue: 'Date' },
     
    ];
  }
  ngOnInit(): void {
    this._iconService.registerIcons([this.icon]);
  }
}

对于 app.component.html


  <h1>hello SVG icon</h1>

  <div>
    <mat-form-field>
      <mat-label>Select a field type</mat-label>
      <mat-select [(ngModel)]="field">
        <mat-select-trigger>
          <mat-icon svgIcon="{{field.value}}"></mat-icon>
          &nbsp;{{field.viewValue}}
        </mat-select-trigger>
        <mat-option *ngFor="let item of allFieldTypes" [value]="item">
          <mat-icon class="material-icons" svgIcon="{{item.value}}"></mat-icon>
          {{item.viewValue}}
        </mat-option>
      </mat-select>
    </mat-form-field>
  </div>

英文:

you seem to have wrong configuration for this service,
here's an update service
and here's folder structure
folder structure

and output here
output


import { Injectable } from &#39;@angular/core&#39;;
import { DomSanitizer } from &#39;@angular/platform-browser&#39;;
import { MatIconRegistry } from &#39;@angular/material/icon&#39;;

@Injectable({
  providedIn: &#39;root&#39;
})
export class IconService {

  constructor(
    private _iconRegistry: MatIconRegistry,
    private _sanitizer: DomSanitizer
  ) {}
  public registerIcons(icons: Array&lt;string&gt;): void {
    icons.forEach((icon) =&gt; {

      this._iconRegistry.addSvgIcon(
        icon,
        this._sanitizer.bypassSecurityTrustResourceUrl(
          `assets/icons/${icon}.svg`
        )
      );
    });
  }
}

for app.module.ts

import { NgModule } from &#39;@angular/core&#39;;
import { BrowserModule } from &#39;@angular/platform-browser&#39;;
import { BrowserAnimationsModule } from &#39;@angular/platform-browser/animations&#39;;
import { AppRoutingModule } from &#39;./app-routing.module&#39;;
import { AppComponent } from &#39;./app.component&#39;;
import { IconService } from &#39;./services/icon.service&#39;;
import { MatIconModule } from &#39;@angular/material/icon&#39;;
import { HttpClientModule } from &#39;@angular/common/http&#39;;
import { MatSelectModule } from &#39;@angular/material/select&#39;;
import { FormsModule, ReactiveFormsModule } from &#39;@angular/forms&#39;;

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    MatIconModule,
    BrowserAnimationsModule,
    HttpClientModule,
    FormsModule,
    MatSelectModule,
    ReactiveFormsModule
  ],
  providers: [IconService],
  bootstrap: [AppComponent]
})
export class AppModule { }


FOR app.component.ts

import { Component, OnInit } from &#39;@angular/core&#39;;
import { IconService } from &#39;./services/icon.service&#39;;

export class AppComponent implements OnInit {
  title = &#39;app-icons&#39;;
  icon: string = &#39;soc-icon&#39;;

  allFieldType=[];

  field:any

  constructor(private _iconService: IconService) {
    this.field = {
      value: this.icon,
      viewValue: &#39;Text&#39;,
    };
    this.allFieldTypes = [
      { value: this.icon, viewValue: &#39;Text&#39; },
      { value: this.icon, viewValue: &#39;Number&#39; },
      { value: this.icon, viewValue: &#39;Date&#39; },
     
    ];
  }
  ngOnInit(): void {
    this._iconService.registerIcons([this.icon]);
  }

for app.compnent.html


  &lt;h1&gt;hello SVG icon&lt;/h1&gt;

  &lt;div&gt;
    &lt;mat-form-field&gt;
      &lt;mat-label&gt;Select a field type&lt;/mat-label&gt;
      &lt;mat-select [(ngModel)]=&quot;field&quot;&gt;
        &lt;mat-select-trigger&gt;
          &lt;mat-icon svgIcon=&quot;{{field.value}}&quot;&gt;&lt;/mat-icon&gt;
          &amp;nbsp;{{field.viewValue}}
        &lt;/mat-select-trigger&gt;
        &lt;mat-option *ngFor=&quot;let item of allFieldTypes&quot; [value]=&quot;item&quot;&gt;
          &lt;mat-icon class=&quot;material-icons&quot; svgIcon=&quot;{{item.value}}&quot;&gt;&lt;/mat-icon&gt;
          {{item.viewValue}}
        &lt;/mat-option&gt;
      &lt;/mat-select&gt;
    &lt;/mat-form-field&gt;
  &lt;/div&gt;

huangapple
  • 本文由 发表于 2023年5月30日 00:30:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定