我试图根据组件中元素的宽度设置计算属性。问题是我不知道如何以反应性的方式获取宽度。
我的元素上有一个 ref 来获取有效的宽度。但是我似乎无法让 vue 检测到宽度正在变化
我将元素设置为:
<div ref="myDiv"></div>
Run Code Online (Sandbox Code Playgroud)
我的计算属性为:
myProperty() {
return this.$refs.myDiv.clientWidth/2;
}
Run Code Online (Sandbox Code Playgroud)
myProperty 计算正确,但不会随着 myDiv 宽度的变化而变化
我有以下html:
<button mat-icon-button #notificationMenuBtn [matMenuTriggerFor]="notificationsMenu">
</button>
<mat-menu #notificationsMenu="matMenu" [overlapTrigger]="false">
</mat-menu>
Run Code Online (Sandbox Code Playgroud)
如何从typescript上访问notificaitonMenuBtn上的thematMenuTriggerFor?我尝试使用View Child(如下所示),但我似乎无法将其绑定到触发器,仅绑定到按钮.
@ViewChild('notificationMenuBtn') notificationMenuBtn : MatMenuTrigger;
this.notificationMenuBtn.openMenu();
Run Code Online (Sandbox Code Playgroud)