[英]Error in Angular with shared Module: generates "appears in XXX.module but itself has errors"
Ionic v6 project with Angular. I have made several directives that I need to use in several pages.带有 Angular 的 Ionic v6 项目。我做了几个我需要在几个页面中使用的指令。 I have created a shared module:
我创建了一个共享模块:
import { MediaPage } from './../directivas/media/media.page';
import { TformPage } from './../directivas/tform/tform.page';
import { ViewformPage } from './../directivas/viewform/viewform.page';
import { TristatePage } from './../directivas/tristate/tristate.page';
import { IonicModule } from '@ionic/angular';
import { BtitleDirective } from './../directivas/btitle.directive';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
declarations: [
BtitleDirective,
TristatePage,
ViewformPage,
TformPage,
MediaPage
],
imports: [
],
exports:[
CommonModule,
IonicModule,
BtitleDirective,
TristatePage,
ViewformPage,
TformPage,
MediaPage
]
})
export class SharedModule { }
I have the following error on each module that uses SharedModule:我在使用 SharedModule 的每个模块上都有以下错误:
SharedModule Appears in the NgModule.imports of HomePageModule, but itself has errors
These errors appear in VSCode and with ionic build, but with ionic serve everything is working well,这些错误出现在 VSCode 和离子构建中,但离子服务一切正常,
Any ideas?有任何想法吗?
I have found a part of the problem.我发现了问题的一部分。 I have a component directive called TriState, showing 3 buttons.
我有一个名为 TriState 的组件指令,显示 3 个按钮。 This is tristate.module.ts:
这是 tristate.module.ts:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';
import { TristatePageRoutingModule } from './tristate-routing.module';
import { TristatePage } from './tristate.page';
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
TristatePageRoutingModule
],
declarations: [TristatePage],
})
export class TristatePageModule {}
tristate.page.ts三态.page.ts
import { UtilService } from './../../services/util.service';
import { Component, Input, Output, EventEmitter, forwardRef, OnInit } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
@Component({
selector: 'app-tristate',
templateUrl: './tristate.page.html',
styleUrls: ['./tristate.page.scss'],
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => TristatePage),
multi: true
}]
})
export class TristatePage implements ControlValueAccessor,OnInit {
@Input() label: string = '';
@Input() readonly: boolean;
@Output() onchange: EventEmitter<string> = new EventEmitter<string>();
value: string ;
onChange: (_: any) => void = (_: any) => {}; //Invoked when the model has been changed
onTouched: () => void = () => {}; // Invoked when the model has been touched
constructor(
public utilS: UtilService
) {}
public ngOnInit(){
console.log('oninit tri',this.value);
this.value='';
}
public setvalue() {
//console.log($event,this.value);
if(this.readonly) return;
//this.value = this.options[p].value;
this.updateChanges();
if(this.onchange)
this.onchange.emit(this.value);
}
/**
* Method that is invoked on an update of a model.
*/
updateChanges() {
if(!this.value) return;
console.log('updchange',this.value);
this.onChange(this.value);
}
///////////////
// OVERRIDES //
///////////////
/**
* Writes a new item to the element.
* @param value the value
*/
writeValue(value: string): void {
console.log('write ',this.value);
if(value)
this.value = value;
this.updateChanges();
}
/**
* Registers a callback function that should be called when the control's value changes in the UI.
* @param fn
*/
registerOnChange(fn: any): void {
this.onChange = fn;
}
/**
* Registers a callback function that should be called when the control receives a blur event.
* @param fn
*/
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
}
And tristate.page.html:和三态.page.html:
<div class="row">
<div class="col-md-8 offset-md-2">
<h3>{{label}}</h3>
<ion-segment (click)="setvalue()" [(ngModel)]="value" >
<ion-segment-button value="S" style="--background-checked:green;" >
Sí
<ion-icon name="{{utilS.conficons.S}}"></ion-icon>
</ion-segment-button>
<ion-segment-button value="P" style="--background-checked:orange">
<ion-icon name="{{utilS.conficons.P}}"></ion-icon>
N/C
</ion-segment-button>
<ion-segment-button value="N" style="--background-checked:red;">
No
<ion-icon name="{{utilS.conficons.N}}"></ion-icon>
</ion-segment-button>
</ion-segment>
</div>
</div>
If I remove TristatePage from module declarations, above errors "SharedModule Appears in the NgModule.imports of...", disappear, but I have a new one in template:如果我从模块声明中删除 TristatePage,上面的错误“SharedModule Appears in the NgModule.imports of...”就会消失,但我在模板中有一个新错误:
Can't bind to 'ngModel' since it isn't a known property of 'ion-segment'.
I don't understand what happens..:(我不明白发生了什么..:(
It's as the error says: ngModel isn't a known property of ion-segment.正如错误所说:ngModel 不是离子段的已知属性。 link to ionic docs
链接到离子文档
The only way I can see to get the selected segment is to use the 'ionChange' event as documented in the ionic docs.我能看到获得选定段的唯一方法是使用离子文档中记录的“ionChange”事件。
In your html:在您的 html 中:
<ion-segment (ionChange)="segmentChanged($event)">
In your typescript:在您的 typescript 中:
segmentChanged(ev: any) {
console.log('Segment changed', ev);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.