Angular CDK 菜单在页面底部打开。

huangapple go评论77阅读模式
英文:

Angular CDK Menu opens at the bottom of the page

问题

I've implemented the CDK Menu from the documentation (doc) in my app.

Here's what I've done:

  1. Install the CDK:
    npm i @angular/cdk

  2. Import the menu cdk in app.module.ts and use it in the "imports" section:

    import { CdkMenuModule } from '@angular/cdk/menu'
    imports: [BrowserModule, CdkMenuModule]
    
  3. Create the menu in my app.component.html:

    <button [cdkMenuTriggerFor]="menu" class="example-standalone-trigger">Click me!</button>
    <ng-template #menu>
      <div class="example-menu" cdkMenu>
        <button class="example-menu-item" cdkMenuItem>Refresh</button>
        <button class="example-menu-item" cdkMenuItem>Settings</button>
        <button class="example-menu-item" cdkMenuItem>Help</button>
        <button class="example-menu-item" cdkMenuItem>Sign out</button>
      </div>
    </ng-template>
    <hello-world></hello-world>
    

The problem is that when I click on the button, the menu opens at the bottom of the page instead of just under the button.

Here's a Stackblitz that shows the problem: Stackblitz Link
I've followed this example from the documentation (their Stackblitz works correctly): menu-with-standalone-trigger

英文:

I've implemented the CDK Menu from the documentation (doc) in my app.

Here's what I've done:

  1. Install the CDK:
    npm i @angular/cdk

  2. Import the menu cdk in app.module.ts and use it in the "imports" section:
    import { CdkMenuModule } from &#39;@angular/cdk/menu&#39;
    imports: [BrowserModule, CdkMenuModule]

  3. Create the menu in my app.component.html

    &lt;button [cdkMenuTriggerFor]=&quot;menu&quot; class=&quot;example-standalone-trigger&quot;&gt;Click me!&lt;/button&gt;
    &lt;ng-template #menu&gt;
      &lt;div class=&quot;example-menu&quot; cdkMenu&gt;
        &lt;button class=&quot;example-menu-item&quot; cdkMenuItem&gt;Refresh&lt;/button&gt;
        &lt;button class=&quot;example-menu-item&quot; cdkMenuItem&gt;Settings&lt;/button&gt;
        &lt;button class=&quot;example-menu-item&quot; cdkMenuItem&gt;Help&lt;/button&gt;
        &lt;button class=&quot;example-menu-item&quot; cdkMenuItem&gt;Sign out&lt;/button&gt;
      &lt;/div&gt;
    &lt;/ng-template&gt;
    &lt;hello-world&gt;&lt;/hello-world&gt;
    

The problem is that when I click on the button, the menu opens at the bottom of the page instead of just under the button.

Here's a Stackblitz that shows the problem: https://stackblitz.com/edit/f1y9wh
I've followed this example from the documentation (their stackblitz works correctly): menu-with-standalone-trigger

答案1

得分: 5

Sure, here's the translated content:

如果有人遇到这个问题,您需要在您的 styles.scss 中导入以下内容:

@import '@angular/cdk/overlay-prebuilt.css';

这是来自 CDK Overlay 的:https://material.angular.io/cdk/overlay/overview
但是不知何故,CDK 菜单文档中没有写明您需要导入这个CSS...

英文:

Ok so if anyone have this problem, you have to import this in your styles.scss:

@import &#39;@angular/cdk/overlay-prebuilt.css&#39;;

It's coming from the CDK Overlay: https://material.angular.io/cdk/overlay/overview
But somehow it's not written in the CDK Menu documentation that you have to import this CSS...

huangapple
  • 本文由 发表于 2023年5月11日 08:01:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定