Har*_*evu 16 font-awesome typescript angular
我用angular-cli(ng new my-project-name)创建了一个新项目
当我运行npm run test它运行没有任何失败.
我在项目中添加了font-awsome模块(https://www.npmjs.com/package/angular-font-awesome)来显示字体图标.
在我的html文件中添加了<fa name="bars"></fa>标签并获得了预期的输出.如果我npm run test再次运行,它将以3个失败结束,所有失败都是针对fa标记.
这是样本失败报告
'fa' is not a known element:
1. If 'fa' is an Angular component, then verify that it is part of this module.
2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("--The content below is only a placeholder and can be replaced.-->
<div style="text-align:center"> [ERROR ->]<fa name="bars"></fa>
<h1> Welcome to {{title}}!
"): ng:///DynamicTestModule/AppComponent.html@2:2 Error: Template parse errors:
at syntaxError home/harsha/Documents/Projects/testProject/node_modules/@angular/compiler/esm5/compiler.js:466:22)
Run Code Online (Sandbox Code Playgroud)
我尝试了一些修正,如添加NO_ERRORS_SCHEMA,CUSTOM_ELEMENTS_SCHEMA在app.module.ts文件中.
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AngularFontAwesomeModule
],
providers: [],
bootstrap: [AppComponent],
schemas: [
CUSTOM_ELEMENTS_SCHEMA,
NO_ERRORS_SCHEMA
]
})`
Run Code Online (Sandbox Code Playgroud)
但没有任何效果.
小智 16
我解决了这个问题,创建了一个名为 SampleComponent(sample.ts) 的自定义组件,我想在welcome.html 中使用它,它位于离子项目中所有自定义组件的通用模块文件下,名称为components.module.ts,如下所示:
import { NgModule,NO_ERRORS_SCHEMA,CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { SampleComponent } from './sample/sample';
@NgModule({
declarations: [SampleComponent],
imports: [],
exports: [SampleComponent],
schemas: [
CUSTOM_ELEMENTS_SCHEMA,
NO_ERRORS_SCHEMA
]
})
export class ComponentsModule {}
Run Code Online (Sandbox Code Playgroud)
在welcome.module.ts 中,我想使用我的sample.component.ts,我导入了components.module.ts,如下所示
import { NgModule } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';
import { IonicPageModule } from 'ionic-angular';
import { WelcomePage } from './welcome';
import { ComponentsModule } from "../../components/components.module";
@NgModule({
declarations: [
WelcomePage,
],
imports: [
IonicPageModule.forChild(WelcomePage),
TranslateModule.forChild(),
ComponentsModule
],
exports: [
WelcomePage
]
})
export class WelcomePageModule { }
Run Code Online (Sandbox Code Playgroud)
Welcome.html我在其中使用了我的自定义组件 (SampleComponent)
<ion-content scroll="false">
<div class="splash-bg"></div>
<div class="splash-info">
<div class="splash-logo"></div>
<div class="splash-intro">
{{ 'WELCOME_INTRO' | translate }}
</div>
</div>
<div padding>
<p>`enter code here`
<sample>loading...</sample>
</p>
<button ion-button block (click)="signup()">{{ 'SIGNUP' | translate }}</button>
<button ion-button block (click)="login()" class="login">{{ 'LOGIN' | translate }}</button>
</div>
</ion-content>
Run Code Online (Sandbox Code Playgroud)
小智 8
在您的测试规范文件中,需要这样设置:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ yourcomponent ],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
}));
Run Code Online (Sandbox Code Playgroud)
请注意TestBed.configureTestingModule方法中的schemas属性。设置schemas属性后,您的测试应该运行无误,就像添加Font Awsome组件之前一样。
同时制定动态组件的方法到我的项目,这是在描述我有同样的错误这个帖子。在我的情况下,错误是通过传递svg标签生成的DynamicComponent。添加NO_ERRORS_SCHEMA到@NgModule在此组件帮助:
import { Component, OnChanges, OnInit, Input, NgModule, NgModuleFactory, Compiler, SimpleChanges, NO_ERRORS_SCHEMA } from '@angular/core';
import { SharedModule } from '../../../../../../../../shared.module';
@Component({
selector: 'dynamic',
template: `<ng-container *ngComponentOutlet="dynamicComponent; ngModuleFactory: dynamicModule;"></ng-container>`
})
export class DynamicComponent {
dynamicComponent: any;
dynamicModule: NgModuleFactory<any>;
@Input('html') html: string;
constructor(private compiler: Compiler) {}
ngOnChanges(changes: SimpleChanges) {
if (changes['html'] && !changes['html'].isFirstChange()) {
this.dynamicComponent = this.createNewComponent(this.html);
this.dynamicModule = this.compiler.compileModuleSync(this.createComponentModule(this.dynamicComponent));
}
}
ngOnInit() {
this.dynamicComponent = this.createNewComponent(this.html);
this.dynamicModule = this.compiler.compileModuleSync(this.createComponentModule(this.dynamicComponent));
}
protected createComponentModule(componentType: any) {
@NgModule({
imports: [SharedModule],
declarations: [componentType],
entryComponents: [componentType],
schemas: [NO_ERRORS_SCHEMA]
})
class RuntimeComponentModule {}
// a module for just this Type
return RuntimeComponentModule;
}
protected createNewComponent(template: string) {
@Component({
selector: 'dynamic-component',
template: template ? template : '<div></div>'
})
class MyDynamicComponent {
//metods passed via dynamic component to svg
testMe() {
alert('test passed!');
}
}
return MyDynamicComponent;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46644 次 |
| 最近记录: |