首页 > 解决方案 > 角度自定义标签不呈现和执行功能

问题描述

我在 html 中有一个自定义标签。在我的 next.component.ts 文件中,

@Component({
  selector: 'nextbutton',
  template: ` <button (click) = "nextfunc()">Next</button> `
})

export class NextComponent{

  nextfunc() {

==>>我的 app.component.html 文件

<div>
<!--other tags-->
<next-button></next-button>

</div>

我无法呈现按钮并访问 nextfunc()

===>>我的 app.module.ts


    import { BrowserModule } from '@angular/platform-browser';
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

import { AppComponent } from './app.component';
import { NextComponent } from './next.component';


@NgModule({
  declarations: [
    AppComponent,
    NextComponent
  ],
  imports: [
    BrowserModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
  schemas: [
    CUSTOM_ELEMENTS_SCHEMA
]
})
export class AppModule { }

===>>我的 next.component.ts

    import { Component } from '@angular/core';

@Component({
  selector: 'nextbutton',
  template: ` <button (click) = "nextfunc()">Next</button> `
})

export class NextComponent{

  nextfunc() {
     //do something
  }
}

标签: htmlangulartypescriptangular-componentscustom-tag

解决方案


当您使用标签时,您的指令/组件中的选择器是下一个按钮

 <next-button></next-button>

将您的选择器更改为下一个按钮或将标签更改为下一个按钮


推荐阅读