简体   繁体   English

Typescript类方法无法识别

[英]Typescript class methods unrecognized

I'm having a problem with using class' methods where they're imported. 我在使用导入类的类方法时遇到问题。

This is a class I have 这是我上的课

cloneable.ts 可克隆

export interface Cloneable<T> {
    clone() : T;
}

task.ts 任务

import { Cloneable } from './cloneable'

export class Task implements Cloneable<Task>  {
    name: string;
    finished: boolean;

    constructor(name: string = "", finished: boolean = false) {
        this.name = name;
        this.finished = finished;
    }

    clone() : Task {
        return new Task(this.name, this.finished);
    }
}

I'm using the Task class in another file 我在另一个文件中使用Task

todoItem.component.ts todoItem.component.ts

import { Component, Input } from '@angular/core';
import { Task } from '../../model/task'

@Component({
  selector: 'todo-item',
  templateUrl: './app/todoItem/todoItem.component.html',
  styleUrls: ['./app/todoItem/todoItem.component.css']
})
export class TodoItemComponent {
  @Input() name: string;
  @Input() tasks: Task[];
  currentEdited: { original: Task, toEdit: Task } = null;

  startEditing = function(task: Task) {
    this.cancelEditing();
    this.currentEdited = { original: task, toEdit: new Task(task.name, task.finished) };  // problematic line
  }

  // uninteresting code
}

The error 错误

http://localhost:3000/model/task Failed to load resource: the server responded with a status of 404 (Not Found)
localhost/:14 Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:3000/model/task(…)(anonymous function) @ localhost/:14

this is localhost/:14 这是localhost /:14

<script>
      System.import('app').catch(function(err){ console.error(err); });
</script>

Notice the line 注意行
this.currentEdited = { original: task, toEdit: new Task(task.name, task.finished) };

There are two options I see to achieve my goal 我看到有两种选择可以实现我的目标

  1. this.currentEdited = { original: task, toEdit: new Task(task.name, task.finished) };
  2. this.currentEdited = { original: task, toEdit: task.clone() };

although, when I'm not using class methods, it works, but I don't create a clone this.currentEdited = { original: task, toEdit: task }; 虽然,当我不使用类方法时,它可以工作,但是我不创建一个克隆this.currentEdited = { original: task, toEdit: task };

This happened to me before, but I didn't find a way to handle this so I just accessed class members, but I guess that I should work this out. 这在我之前发生过,但是我没有找到解决这个问题的方法,所以我只访问了类成员,但是我想应该解决这个问题。

This is my tryout angular2 project, just getting to know angular2. 这是我的试用angular2项目,只是了解angular2。

In file todoItem.component.ts there is a script reference error, one to many . 在文件todoItem.component.ts中,存在一个脚本引用错误,一对多. in the leading path. 在领先的道路上。

import { Task } from './../model/task'

or if it really is another directory down then 或者如果它确实是另一个目录,那么

import { Task } from './../../model/task'

Bot not this because you you should prefix with ./ 不要这样,因为您应该以./前缀

import { Task } from '../../model/task'

Which is why the error reads that the file cannot be found (404) 这就是为什么错误显示无法找到文件的原因(404)

http://localhost:3000/model/task Failed to load resource: the server responded with a status of 404 (Not Found) http:// localhost:3000 / model / task加载资源失败:服务器响应状态为404(未找到)

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

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