【问题标题】:Refused to load the font - Angular 2拒绝加载字体 - Angular 2
【发布时间】:2017-03-28 19:54:25
【问题描述】:

我从 Angular 2 开始,一直在玩路线。 当我没有路线时,一切正常,但现在每次我去 /home 时,我都会收到此错误。

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.

我正在使用 HAML,但我认为这不是问题所在。 索引仍然是 .html 扩展名,似乎也没有引起问题。

这是我的 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 {}

当我转到 localhost:4200 时,redirectTo /home 有效。 如果我去 localhost:4200/home,那是当我得到描述的错误并且我的页面卡在 App Loading... 屏幕中。

如果有帮助,我会使用 angular-cli 生成应用程序:

ng new application

如果有人能帮我解决这个问题,那就太棒了,提前谢谢你。

我希望这是足够的信息。如果没有,请告诉我您还需要知道什么。

【问题讨论】:

  • 在 angular cli 中可能是一些字体选择。不确定,但你可以试试

标签: angular routing


【解决方案1】:

添加content security policy

例如font-src 使用 data:

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

如果您使用 CDN,请根据需要添加,例如对于fonts.gstatic.com

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; font-src 'self' data: fonts.gstatic.com;">
猜你喜欢
  • 2012-04-01
  • 1970-01-01
  • 2018-02-06
  • 2016-04-28
  • 2020-12-14
  • 1970-01-01
  • 2018-08-15
  • 2017-04-04
  • 1970-01-01
相关资源
最近更新 更多