【问题标题】:Can't bind to 'property' as it is not a known property of 'selector'无法绑定到“属性”,因为它不是“选择器”的已知属性
【发布时间】:2020-06-21 06:56:37
【问题描述】:

我正在尝试在我的项目的不同模块中使用子组件,但我收到错误“无法绑定到‘消息’,因为它不是‘or-app-wysiwyg’的已知属性。我看不到我的导入有任何问题,并且我检查了我的表单模块是否已正确添加。我认为问题与选择器有关,但到目前为止我所尝试的一切都没有消除错误。

Wysiwyg.Component.ts

import { Component, OnInit} from '@angular/core';
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';

@Component({
selector: 'or-app-wysiwyg',
templateUrl: './wysiwyg.component.html',
styleUrls: ['./wysiwyg.component.scss']
})

export class WysiwygComponent implements OnInit {

public Editor = ClassicEditor;

message = '';
constructor() { }

ngOnInit() {
 }
}

Wysiwyg.Component.html

<ckeditor [(ngModel)]="message" [editor]="Editor"></ckeditor>

Notes.Component.ts

import { Component, OnInit, ChangeDetectorRef, Input } from '@angular/core';
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import { Note } from '../../../../../core/src/models/note';
import { NoteOutgoing } from 'projects/core/src/models/noteOutgoing';
import { AuthService } from 'core';
import { NotesService } from 'projects/core/src/services/note.service';

  @Component({
  selector: 'app-notes',
  templateUrl: './notes.component.html',
  styleUrls: ['./notes.component.scss'],
  })

export class NotesComponent implements OnInit {

  @Input() message: any;

  public Editor = ClassicEditor;
  notes: Note[] = [];
  noteOutgoing: NoteOutgoing = {message: ''};

  constructor(private noteService: NotesService, private authService: AuthService,
              private cdr: ChangeDetectorRef) {}

  ngOnInit() {
  this.getNotes();
  }

  getNotes() {
    this.noteService.getNotesFromDB().subscribe(result => {
    this.notes = result;
    this.cdr.detectChanges();
    }, error => console.error(error));
  }

  addNote() {
    const name = this.authService.getUser().firstName + ' ' + this.authService.getUser().lastName;
    const note: Note = {name, message: this.noteOutgoing.message, date: 'date', tags: 'tag'};
    this.notes.push(note);
    console.log(this.notes);
    this.noteService.addNoteToDB(this.noteOutgoing).subscribe(() => {
    }, error => console.error(error));
    this.noteOutgoing.message = '';
  }
}

Note.Module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NotesComponent } from './notes.component';
import { CKEditorModule } from '@ckeditor/ckeditor5-angular';
import { FormsModule } from '@angular/forms';
import { WysiwygComponent } from 'projects/core/src/components/wysiwyg/wysiwyg.component';

@NgModule({
  imports: [
    CommonModule,
    CKEditorModule,
    FormsModule
  ],
  declarations: [NotesComponent, WysiwygComponent]
})
export class NotesModule {}

Notes.Component.html

<or-app-wysiwyg [message]="message"></or-app-wysiwyg>

<button type="button" (click)="addNote()" class="btn btn- 
default">Save</button>

<div class= "card m-3" *ngFor= "let note of notes">
 Name: {{note.name}} <br> Note: {{note.message}} <br> Date: {{note.date}} <br> 
 Tags: {{note.tags}}
</div>

【问题讨论】:

    标签: javascript angular module components


    【解决方案1】:

    你的错误信息是这样的:

    '不能绑定到'message',因为它不是'or-app-wysiwyg'的已知属性

    您尚未发布违规代码,但错误消息表明您正在尝试绑定到元素 or-app-wysiwyg 上的 message 属性,如下所示:

    <or-app-wysiwyg [message]="message">
    </or-app-wysiwyg>
    

    为此,您需要将message 属性指定为@Input() 属性。

    Wysiwyg.component.ts

    export class WysiwygComponent implements OnInit {
      public Editor = ClassicEditor;
    
      @Input() message: string;
    }
    

    【讨论】:

    • 抱歉,用相关代码更新了我的问题。解决了我的问题,谢谢
    猜你喜欢
    • 2017-02-03
    • 2020-01-09
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 2019-11-04
    • 2018-07-04
    • 2023-02-01
    相关资源
    最近更新 更多