javascript - ionic 3、自定义组件和ngClass绑定(bind)问题

标签 javascript angular typescript ionic3

我尝试在 Ionic3 中制作一个简单的组件来显示点来指示我们所在的页面。 我使用 ionicgenerate 命令来生成此组件:

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

@Component({
  selector: 'panel-indicator',
  templateUrl: 'panel-indicator.html'
})
export class PanelIndicatorComponent {

  @Input() page: number;

  constructor() {
    console.log('Hello PanelIndicatorComponent Component');
    this.page = 1;
  }

}

这是模板部分:

<div class="indicator-list">
  <div class="indicator selected" [ngClass]="{'selected': page === 1}"></div>
  <div class="indicator" [ngClass]="{'selected': page === 2}"></div>
  <div class="indicator" [ngClass]="{'selected': page === 3}"></div>
</div>

当我删除 ngclass 属性时,没有问题。但是,当我添加它时,出现以下错误:

Can't bind to 'ngClass' since it isn't a known property of 'div'. ("
<div class="indicator-list">

  <div class="indicator selected" [ERROR ->][ngClass]="{'selected': page === 1}"></div>

  <div class="indicator" [ngClass]="{'selected': page =="): ng:///ComponentsModule/PanelIndicatorComponent.html@4:34
Can't bind to 'ngClass' since it isn't a known property of 'div'. ("iv class="indicator selected" [ngClass]="{'selected': page === 1}"></div>

  <div class="indicator" [ERROR ->][ngClass]="{'selected': page === 2}"></div>

  <div class="indicator" [ngClass]="{'selected': page =="): ng:///ComponentsModule/PanelIndicatorComponent.html@6:25
Can't bind to 'ngClass' since it isn't a known property of 'div'. ("
  <div class="indicator" [ngClass]="{'selected': page === 2}"></div>

  <div class="indicator" [ERROR ->][ngClass]="{'selected': page === 3}"></div>

</div>
"): ng:///ComponentsModule/PanelIndicatorComponent.html@8:25
    at syntaxError (http://localhost:8100/build/vendor.js:101030:34)
    at TemplateParser.parse (http://localhost:8100/build/vendor.js:124893:19)
    at JitCompiler._parseTemplate (http://localhost:8100/build/vendor.js:134321:37)
    at JitCompiler._compileTemplate (http://localhost:8100/build/vendor.js:134296:23)
    at http://localhost:8100/build/vendor.js:134198:62
    at Set.forEach (<anonymous>)
    at JitCompiler._compileComponents (http://localhost:8100/build/vendor.js:134198:19)
    at http://localhost:8100/build/vendor.js:134068:19
    at Object.then (http://localhost:8100/build/vendor.js:101019:77)
    at JitCompiler._compileModuleAndComponents (http://localhost:8100/build/vendor.js:134067:26)

我没有找到任何关于这个问题的信息。因此,如果您有想法,欢迎您。

感谢您的阅读。

最佳答案

您需要添加BrowserModule

@NgModule(
  imports: [BrowserModule, /* other imports */] from '@angular/platform-browser',
  ...
)

如果它在子模块中,则导入 CommonModule。

import { CommonModule } from "@angular/common"

关于javascript - ionic 3、自定义组件和ngClass绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49881867/

相关文章:

javascript - 在 Angular Virtual Scroll 中使用父滚动条而不是子组件滚动条

dependency-injection - 如何创建依赖于 ionic 2/angular 2 并使用依赖注入(inject)的库

Angular 7路由连接导致编译错误

javascript - 如何在 D3.js 版本 4 中执行节点居中?

javascript - Bootstrap 3 滚动 spy

javascript - 将数组映射到数组 Js 中

typescript - Intellisense 卡在用 typescript 加载

javascript - 使用 JavaScript 设置 cookie 和获取 cookie

javascript - 无法在 typescript 版本 3.4.5 中使用 BigInt。错误 TS2304 : Cannot find name 'BigInt'

angular - ionic 3 : Modal Controller - Get component Instance