【问题标题】:Angular custom tag not rendering and executing function角度自定义标签不呈现和执行功能
【发布时间】:2020-02-14 06:13:30
【问题描述】:

我在 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
  }
}

【问题讨论】:

  • 将选择器更改为nextbutton to next-button
  • 成功了。谢谢。也许本地主机渲染很慢

标签: html angular typescript angular-components custom-tag


【解决方案1】:

您在指令/组件中的选择器是 nextbutton,而您将标签用作

 <next-button></next-button>

将您的选择器更改为 next-button 或将标签更改为 nextbutton

【讨论】:

  • 成功了。谢谢。也许本地主机渲染很慢
猜你喜欢
  • 1970-01-01
  • 2011-03-08
  • 2019-03-08
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
  • 2018-03-05
  • 2012-12-11
  • 1970-01-01
相关资源
最近更新 更多