Angular 11使用angular-sticky-things报错:Can't bind to boundary since it isn't a known property of div 及相关语法咨询
stickyThing指令的语法及错误解决 首先,咱们先明确你提到的语法名称:
你写的 <div stickyThing [spacer]="spacer" [boundary]="boundary" [enable]="true"> 是**Angular属性型指令(Attribute Directive)**的调用写法:
stickyThing是指令的属性选择器(对应指令类中@Directive({ selector: '[stickyThing]' })的定义),用来标记这个div元素要应用该指令;[spacer]、[boundary]、[enable]是该指令的**@Input输入属性**,用来向指令传递数据。
接下来解决你遇到的错误:'can't bind to boundary since it isn't a known property of div'
这个错误的核心原因是:Angular没有识别到boundary是stickyThing指令的输入属性——本质是你没有正确导入该指令所属的模块,导致Angular不知道这个指令的存在,误把boundary当成了div的原生属性(而div并没有这个原生属性)。
解决方案:
导入指令所属的模块
这个stickyThing是第三方库的指令,你需要在使用它的组件所属的NgModule(比如AppModule或者你的业务FeatureModule)中,导入StickyThingsModule并添加到imports数组中:import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { StickyThingsModule } from '@w11k/angular-sticky-things'; import { YourComponent } from './your.component'; // 你使用该指令的组件 @NgModule({ declarations: [YourComponent], imports: [ BrowserModule, StickyThingsModule // 必须添加这一行 ], bootstrap: [YourComponent] }) export class YourModule { }确认库版本与Angular 11兼容
由于你使用的是Angular 11,需要确保安装的@w11k/angular-sticky-things版本是兼容Angular 11的。你可以在package.json中检查版本,或者重新安装对应版本:npm install @w11k/angular-sticky-things@^11.0.0 --save检查输入属性拼写
虽然可能性较低,但可以对照库的文档确认boundary等输入属性的拼写是否正确,避免因拼写错误导致Angular无法识别。
内容的提问来源于stack exchange,提问作者Think Tank

