Refused to load the font - Angular 2

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

I am starting on Angular 2 and have been playing around with routes. Everything was working correctly when I did not have routes, but now every time I go to /home, I get this error.

Refused to load the font 'data:font/woff;base64,d09GRgABAAAAAIw4ABEAAAABQcAAAQABAAAAAAAAAAAAAAAAAAAAA…hgZiCKVViwAiVhsAFFYyNisAIjRLMJCgMCK7MLEAMCK7MRFgMCK1myBCgGRVJEswsQBAIrAA==' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'font-src' was not explicitly set, so 'default-src' is used as a fallback. 

I am working with HAML but I don't think that is the problem. Index is still .html extension which also doesn't seem to be causing problems.

Here is my index.html.

<html>   <head>     <meta charset="UTF-8">     <title>Freelance Bootcamp</title>     <base href='/'>     <meta name="viewport" content="width=device-width, initial-scale=1">   </head>    <body>     <app>App Loading...</app>   </body> </html> 

app.module.ts

import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { HttpModule }    from '@angular/http';  import { AppComponent } from './app.component'; import { HomepageComponent } from './homepage/homepage.component';  import { AppRoutingModule } from './app-routing.module';  @NgModule({     imports: [         BrowserModule,     HttpModule,         AppRoutingModule     ],     declarations: [         AppComponent,         HomepageComponent     ],     bootstrap: [         AppComponent     ] }) export class AppModule {} 

app.component.ts

import { Component } from '@angular/core';  @Component({     moduleId: module.id,     selector: 'app',     templateUrl: 'app.component.haml' }) export class AppComponent {     title: 'Freelance Bootcamp Dashboard'; } 

homepage.component.ts

import { Component } from '@angular/core';  @Component({     moduleId: module.id,     selector: 'homepage',     templateUrl: 'homepage.component.haml' }) export class HomepageComponent {} 

app-routing.module.ts

import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router';  import { HomepageComponent } from './homepage/homepage.component';  const routes: Routes = [     { path: '', redirectTo: '/home', pathMatch: 'full' },     { path: 'home', component: HomepageComponent }, ]  @NgModule({     imports: [ RouterModule.forRoot(routes) ],     exports: [ RouterModule ] }) export class AppRoutingModule {} 

The redirectTo /home works when I go to localhost:4200. If I go to localhost:4200/home, that's when I get the error described and my page gets stuck in the App Loading... screen.

If it helps, I generated the application with angular-cli by typing:

ng new application 

If anyone could help me with this, that would be amazing and thank you in advance.

I hope this is enough information to go off of. If not, please let me know what else you need to know.

回答1:

Add a content security policy.

E.g. font-src to use data:

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; font-src 'self' data:;"> 

And if you use CDNs, add as needed, e.g. for fonts.gstatic.com:

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; font-src 'self' data: fonts.gstatic.com;"> 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!