【问题标题】:Unsure of how to use custom TypeScript class in an angular component不确定如何在 Angular 组件中使用自定义 TypeScript 类
【发布时间】:2020-02-27 05:16:06
【问题描述】:

我有一个组件 GameBoardComponent 我想要一个 Box,它是 TypeScript 类。我最初试图让它像“class Box {}”,但我读到你不能这样做,所以我尝试了“export class Box(){}”,现在看起来像:

import {GameBoardComponent} from './game-board/game-board.component';
import * as p5 from 'p5';
export class Box {
    x: number;
    y: number;
    width: number;
    height: number;
    board: GameBoardComponent;
    constructor(x: number,y: number, width:number) {
        this.width = width;
        this.height = width;
        this.x=1;
        this.y=1;

    }
    draw(canvas: p5): void {
        canvas.rect(this.x,this.y,this.width,this.height);
    };

}

我的游戏板看起来像:

import { Component, OnInit } from '@angular/core';
import { Box } from '../Box';

import * as p5 from 'p5';



/** 
*@todo Game Board
*@body Complete the gameboard, possible integration with other smaller components ie a Tetris component or a Box component
*/
@Component({
  selector: 'app-game-board',
  template: `
    <p>
      game-board works!
    </p>
  `,
  styleUrls: ['./game-board.component.scss']
})
export class GameBoardComponent implements OnInit {
  height: number;
  width: number;
  canvas: p5;
  test: Box;
  constructor() { }

  ngOnInit() {
    this.height = 100;
    this.width = 20;
    this.test = new Box(1,1,this.canvas.width);


    const sketch = (s) => {



      s.preload = () => {
        // preload code
      }

      s.setup = () => {
        s.createCanvas(window.innerWidth/4, window.innerHeight*.8);
      };

      s.draw = () => {
        s.scale(1, -1);
        s.translate(0, -s.height);
        s.background(0);
        s.rect(100, 100, 100, 100);
        s.test.draw();
      };

      s.windowResized = () => {
        s.resizeCanvas(window.innerWidth/4, window.innerHeight*.8);
      };
    }

    this.canvas = new p5(sketch);

  }

}

我的 app.module 看起来像:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {MatButtonModule} from '@angular/material/button';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { GameBoardComponent } from './game-board/game-board.component';
import { TetrisPieceComponent } from './tetris-piece/tetris-piece.component';
import { HoldBoxComponent } from './hold-box/hold-box.component';
import { ViewBoxComponent } from './view-box/view-box.component';
import { MainMenuComponent } from './main-menu/main-menu.component';
import { Box } from './Box';

@NgModule({
  declarations: [
    AppComponent,
    GameBoardComponent,
    TetrisPieceComponent,
    HoldBoxComponent,
    ViewBoxComponent,
    MainMenuComponent,
    Box
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    MatButtonModule
  ],
  providers: [

  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

我在尝试编译时收到以下错误:

Uncaught Error: Unexpected value 'Box' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@Component annotation.

我在谷歌上广泛搜索了该错误并尝试将 Box 在 app.module 中的其他位置从声明移动到提供程序,但它给出了不同的错误并且无法解析构造函数类型。所以也许这就是我的问题?无论如何,我早上的大部分时间都在试图弄清楚在 Angular 组件中使用我自己的打字稿类的正确方法是什么,但我还没有找到任何可行的方法。

任何帮助将不胜感激。

【问题讨论】:

  • 从声明数组中删除Box,就像错误提示的那样,您的类没有提到任何注释。
  • 刚刚意识到我的盒子创建顺序错误,应该是在画布创建之后,但无论如何我都会收到错误消息。 @AJT82 修复了它。抱歉,我对 angular 和 typescript 很陌生,显然我正在阅读您需要在 app.module 中导入所有内容,但我认为这仅适用于组件。
  • 是的,它适用于组件......就像你得到的错误一样,Pipes 和 Directives。这些应该在declarations 数组中声明:)

标签: angular typescript


【解决方案1】:

没有必要在app.module.ts 中声明Box,因为它不是模块、组件、指令或管道。所以只需从app.module.ts 中删除Box

【讨论】:

  • 感谢您在尝试实例化实例时修复了它以及其他错误。
猜你喜欢
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
  • 2017-06-27
  • 2019-06-13
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多