angular - Angular 工具栏固定在顶部
问题描述
我有一个由我和其他组件创建的工具栏。在 app.component 我有这样的东西:
<app-toolbar>
</app-toolbar>
<main>
<a [routerLink]="['/login/en']"></a>
<router-outlet></router-outlet>
</main>
我想使用“position=sticky”以便在滚动期间工具栏始终位于顶部,但我不能使用它,因为我没有每个组件的工具栏。我正在使用 Angular 5。有简单的方法吗?谢谢。
解决方案
您可以创建一个“模板组件”,其中包含工具栏 + a <ng-content>
。
在示例中,我们假设您创建了一个带有选择器的模板,例如<app-toolbar-template>
工具栏模板.ts
@Component({
selector: 'app-toolbar-template',
templateUrl: './toolbar-template.html',
styleUrls: ['./toolbar-template.css']
})
export class ToolbarTemplateComponent {}
工具栏-template.html
<div id="toolbar">Fake toolbar</div>
<ng-content></ng-content>
然后,当您创建组件时:
- 如果您需要工具栏,请将内容放在
<app-toolbar-template>
标签内 - 如果您不需要工具栏,只需放置您的内容。
- 工具栏管理的 CSS 将写在
toolbar-template.css
foo.component.html
<!-- With the toolbar -->
<app-toolbar-template>
<div>Content</div>
</app-toolbar-template>
OR just
<!-- Without the toolbar -->
<div>Content</div>
工作演示在这里
推荐阅读
- android - Android Studio 中的文件与文件资源管理器中的文件不匹配
- rabbitmq - 我应该创建一些交易所 Rabbit MQ 吗?
- elasticsearch - 将 Elasticsearch 数据保留限制在磁盘空间以下
- javascript - Gmail api - 浏览器快速入门错误:错误请求
- laravel - Laravel Passport 未使用 JWT cookie 进行身份验证(自用 API)
- javascript - aws s3 sdk 是否具有与 aws s3 cp 相同或等效的命令?
- laravel - Laravel 主键不允许为空
- excel - 用于对多列求和的最佳公式从搜索值返回 1 行
- python - 在 python 中使用 concat 运算符创建列表的复杂性
- python - 使用 Python 的 RabbitMQ:使用 executemany() 时分别确认和拒绝消息