简体   繁体   English

即使函数存在,角度也会引发错误

[英]Angular throws error even though the function exists

This error is getting me stumped. 此错误使我感到困惑。 Even though the addDocument function is there on DocumentSservice, I do not understand what it means by 'typeof DocumentSservice'. 即使DocumentSservice上有addDocument函数,我也不明白“ typeof DocumentSservice”的含义。 The error is happening on the function onSubmit(). 该错误发生在函数onSubmit()上。 I am also just learning Angular too, so I don't know a whole lot when it gives me those kind of errors. 我也只是在学习Angular,所以当我遇到这类错误时,我并不了解很多。

This is the document-edit.component file that is giving me errors. 这是给我错误的document-edit.component文件。

import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { DocumentsService } from '../documents.service';
import { Document } from '../document.model'
import { NgForm } from '@angular/forms'

@Component({
  selector: 'cms-document-edit',
  templateUrl: './document-edit.component.html',
  styleUrls: ['./document-edit.component.css']
})
export class DocumentEditComponent implements OnInit {
  @ViewChild('f') dlForm: NgForm;
  id: string;
  originalDocument: Document;
  document: Document;
  editMode = false;
  addDocument: DocumentsService;

  constructor(private documentService: DocumentsService,
              private router: Router,
              private route: ActivatedRoute) { }

  ngOnInit() {
    this.route.params
      .subscribe(
        (params: Params) => {
          this.id = params['id'];
          // this.editMode = params['id'] != null;
          if (!this.id){
            this.editMode = false;
            return;
          }

          this.originalDocument = this.documentService.getDocument(this.id);

          if(!this.originalDocument){
           return;
          }

          this.editMode = true;

          this.document = JSON.parse(JSON.stringify(this.originalDocument));
  });
  }

  onSubmit(form:NgForm){
    const values = form.value;
    const newDocument = new Document(values.id, values.name, values.description, values.url);
    if (this.editMode = true){
      this.documentService.updateDocument(this.originalDocument, newDocument);
    }else{
      DocumentsService.addDocument.push(newDocument);
    }
    this.editMode = false;
    form.reset();
    this.onCancel();
    this.router.navigate['/documents'];
  }

  onCancel(){
    this.router.navigate(['/documents']);
  }
}

This is my documentsService: 这是我的documentsService:

import { Injectable, EventEmitter } from '@angular/core';
import { MOCKDOCUMENTS } from './MOCKDOCUMENTS';
import { Document } from './document.model';
import { Subject } from 'rxjs';



@Injectable({
  providedIn: 'root'
})

export class DocumentsService {
  documentSelectedEvent = new EventEmitter<Document>();
  documentListChangedEvent = new Subject<Document[]>();
  documentsChanged = new Subject<Document[]>();
  maxDocumentId: number;
  documents: Document[] = [];
  id: number;

  constructor() {
    this.documents = MOCKDOCUMENTS;
    this.maxDocumentId = this.getMaxId();
  }


  getMaxId(): number{
   var maxId = 0;
    for(let document of this.documents){
     var currentId = document.id;
      if(+currentId > maxId){
       +maxId;
      }
    }
    return maxId;
  }

  getDocuments(): Document[] {
    return this.documents.slice();
  }
  getDocument(index: string) {
    return this.documents[index];
  }

  deleteDocument(document: Document) {
    if (document === null) {
      return;
    }

    const pos = this.documents.indexOf(document);
    if (pos < 0) {
      return;
    }

    this.documents.splice(pos, 1);
    this.documentListChangedEvent.next(this.documents.slice());
  }

  updateDocument(originalDocument: Document, newDocument: Document) {
    if (!originalDocument || !newDocument){
      return;
    }

    const pos = this.documents.indexOf(originalDocument);

    if ( pos < 0){
      return;
    }


    this.documents[pos] = newDocument;

  }

  addDocument(documents: Document[]) {


    this.documents.push(...documents);
    this.documentsChanged.next(this.documents.slice());
  }
}

As you can see in my service app for my documents, you can clearly see that the function is there. 如您在我的服务应用程序中看到的文档所示,您可以清楚地看到该功能在那里。 I also have been looking everywhere for an answer but I didn't find any. 我也一直在到处寻找答案,但是没有找到答案。 Do I have to make a global variable to add the function? 我是否必须使全局变量添加功能? Or is it because I don't have newDocument in my addDocument() function in my documentsService service component? 还是因为在我的documentsService服务组件的addDocument()函数中没有newDocument

The way you have it: DocumentsService.addDocument.push(newDocument); 您拥有的方式: DocumentsService.addDocument.push(newDocument); means that, addDocument is a property, not a function and is of type array. 表示addDocument是属性而不是函数,并且是数组类型。 Neither is the case, therefore the error. 都不是,因此是错误。

What you may want is: either: 您可能想要的是:

DocumentsService.getDocument().push(newDocument);

Or, 要么,

DocumentsService.addDocument([newDocument]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM