gpt4 book ai didi

Angular 嵌套树看起来不符合预期

转载 作者:行者123 更新时间:2023-12-02 18:30:01 31 4
gpt4 key购买 nike

我使用 Angular Nested Tree 组件创建了一个树结构,但树的用户界面看起来不符合预期。例如,“水果”与“扩展更多”一词重叠,而“蔬菜”与“扩展更多”一词重叠,因此这些单词都不清晰。有人可以帮助解决此用户界面问题吗,

这是我的代码

import {NestedTreeControl} from '@angular/cdk/tree';
import {Component} from '@angular/core';
import {MatTreeNestedDataSource} from '@angular/material/tree';

/**
* Food data with nested structure.
* Each node has a name and an optiona list of children.
*/
interface FoodNode {
name: string;
children?: FoodNode[];
}

const TREE_DATA: FoodNode[] = [
{
name: 'Fruit',
children: [
{name: 'Apple'},
{name: 'Banana'},
{name: 'Fruit loops'},
]
}, {
name: 'Vegetables',
children: [
{
name: 'Green',
children: [
{name: 'Broccoli'},
{name: 'Brussel sprouts'},
]
}, {
name: 'Orange',
children: [
{name: 'Pumpkins'},
{name: 'Carrots'},
]
},
]
},
];

/**
* @title Tree with nested nodes
*/
@Component({
selector: 'tree-nested-overview-example',
templateUrl: 'tree-nested-overview-example.html',
styleUrls: ['tree-nested-overview-example.css'],
})
export class TreeNestedOverviewExample {
treeControl = new NestedTreeControl<FoodNode>(node => node.children);
dataSource = new MatTreeNestedDataSource<FoodNode>();

constructor() {
this.dataSource.data = TREE_DATA;
}

hasChild = (_: number, node: FoodNode) => !!node.children && node.children.length > 0;
}
.example-tree-invisible {
display: none;
}

.example-tree ul,
.example-tree li {
margin-top: 0;
margin-bottom: 0;
list-style-type: none;
}
<mat-tree [dataSource]="dataSource" [treeControl]="treeControl" class="example-tree">
<!-- This is the tree node template for leaf nodes -->
<mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle>
<li class="mat-tree-node">
<!-- use a disabled button to provide padding for tree leaf -->
<button mat-icon-button disabled></button>
{{node.name}}
</li>
</mat-tree-node>
<!-- This is the tree node template for expandable nodes -->
<mat-nested-tree-node *matTreeNodeDef="let node; when: hasChild">
<li>
<div class="mat-tree-node">
<button mat-icon-button matTreeNodeToggle
[attr.aria-label]="'toggle ' + node.name">
<mat-icon class="mat-icon-rtl-mirror">
{{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
</mat-icon>
</button>
{{node.name}}
</div>
<ul [class.example-tree-invisible]="!treeControl.isExpanded(node)">
<ng-container matTreeNodeOutlet></ng-container>
</ul>
</li>
</mat-nested-tree-node>
</mat-tree>

最佳答案

您可能没有将 MatButtonModule 包含到您的 app.module.ts 文件中。这就是我所缺少的。

import { MatInputModule, MatIconModule, MatButtonModule} from '@angular/material';
import {MatTreeModule} from '@angular/material/tree';

...

@NgModule({
declarations: [
...
],
imports: [
...
MatTreeModule,
MatIconModule,
MatButtonModule,
...


],
providers: [],
bootstrap: [...]
})

关于 Angular 嵌套树看起来不符合预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55284847/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com