简体   繁体   English

不确定如何在 angular 组件中使用自定义 TypeScript class

[英]Unsure of how to use custom TypeScript class in an angular component

I have a Component GameBoardComponent that I want to have a Box, which is TypeScript class.我有一个组件 GameBoardComponent,我想要一个 Box,它是 TypeScript class。 I originally tried to just make it like "class Box {}", but I read you can't do that so I tried "export class Box(){}" which now looks like:我最初试图让它像“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);
    };

}

My gameboard looks like:我的游戏板看起来像:

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);

  }

}

my app.module looks like:我的 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 { }

And I get the following error when trying to compile:尝试编译时出现以下错误:

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

I googled the error extensively and tried to move Box elsewhere in app.module from Declarations to providers but it gives a different error and can't resolve the constructor types.我在谷歌上广泛搜索了这个错误,并试图将 app.module 中的 Box 从声明移到提供者的其他位置,但它给出了不同的错误并且无法解析构造函数类型。 So maybe thats my problem?所以也许这就是我的问题? Regardless I have spent the better part of a my morning trying to figure out what the right way to use my own typescript class in an angular component is and I haven't found anything that works.不管我花了一大半早上的时间试图找出在 angular 组件中使用我自己的 typescript class 的正确方法是什么,我还没有找到任何可行的方法。

Any help would be appreciated.任何帮助,将不胜感激。

There is no need to declare Box in your app.module.ts as it is not module, component, directive or pipe.无需在app.module.ts中声明Box ,因为它不是模块、组件、指令或 pipe。 So just remove Box from app.module.ts .所以只需从app.module.ts中删除Box

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

相关问题 不确定如何对自定义组件在Angular上应用双向绑定 - Unsure how to apply two-way binding on Angular for a custom component 如何在Angular应用程序组件中使用Typescript类方法 - How to use typescript class method inside Angular app component 如何在Angular 2中使用带有typeScript的bower_component - How to use bower_component with typeScript in Angular 2 如何在Angular 7 / Typescript中使用变量类名? - How to use variable class name in Angular 7 / Typescript? 如何通过 Angular 自定义组件传递 class - How to pass class through an Angular Custom component 如何在Angular 2中使用PipeTransform中的自定义组件? - How to use custom component inside PipeTransform in Angular 2? 如何在Angular 2中使用一个类(组件)的变量在另一个类(组件)中? - How to use variable of one class(component) in another class(component) in Angular 2? 如何在 angular 2 组件打字稿文件中添加类属性? - How to add class attribute in angular 2 component typescript file? 如何在 Angular Z558B544CF685F39D34EZ90B3E 中使用 JavaScript function 组件? - How can I use a JavaScript function in a Angular TypeScript component? 如何在TypeScript组件中使用Observable中的值 角度4 - How to use value from Observable inside TypeScript component | Angular 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM