您好,登錄后才能下訂單哦!
這篇文章主要介紹了Angular如何實現二級導航欄,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體內容如下
1、將菜單放入數據庫:
模擬放到該路徑下:
src/assets/json/header.json
{ "siteName": "PGG娛樂健身中心", "menu":[ { "id":"1", "menuName":"首頁", "menuChildren": [{}], "showSubMenu": false }, { "id":"2", "menuName":"健身中心", "menuChildren": [ { "itemId": "1", "menuChidrenItem": "居室器械健身" }, { "itemId": "2", "menuChidrenItem": "野外運動" }, { "itemId": "3", "menuChidrenItem": "健身小知識" } ], "showSubMenu": false }, { "id":"3", "menuName":"休閑娛樂", "menuChildren": [ { "itemId": "1", "menuChidrenItem": "養生釣魚" }, { "itemId": "2", "menuChidrenItem": "野炊燒烤" }, { "itemId": "3", "menuChidrenItem": "真人野戰" } ], "showSubMenu": false }, { "id":"4", "menuName":"訂單中心", "menuChildren": [ { "itemId": "1", "menuChidrenItem": "所有訂單" }, { "itemId": "2", "menuChidrenItem": "已完成訂單" }, { "itemId": "3", "menuChidrenItem": "未完成訂單" } ], "showSubMenu": false }, { "id":"5", "menuName":"個人中心", "menuChildren": [ { "itemId": "1", "menuChidrenItem": "用戶信息修改" } ], "showSubMenu": false } ] }
ts接受數據,并處理:
import { Component, OnInit } from '@angular/core'; import { HttpClient, HttpClientModule } from '@angular/common/http'; @Component({ selector: 'app-header', templateUrl: './header.component.html', styleUrls: ['./header.component.css'] }) export class HeaderComponent implements OnInit { headData: any; constructor( private http: HttpClient ) { } ngOnInit(): void { // http://localhost:4200/assets/json/header.json 可訪問 this.http.get('/assets/json/header.json').subscribe(data => { this.headData = data; console.log(this.headData.menu); }); } showSubMenu(item: any, index: any): void { // 設置當前子菜單顯示 item.showSubMenu = true; } notShowSubMenu(item: any, index: any): void { // 設置當前子菜單不顯示 item.showSubMenu = false; } }
html顯示控制,利用ngstyle控制:
<div class="menu_container"> <div id="top_title">{{headData.siteName}}</div> <div id="menu" *ngFor="let item of headData.menu, let i = index"> <!-- 第一個參數為類名稱,第二個參數為boolean值,如果為true就添加第一個參數的類--> <ul [ngClass]="{'sumMenu': item.showSubMenu}"> <!--mouseleave mouseout供選則--> <li class="top_nav nav_1" (mouseenter)="showSubMenu(item ,i)" (mouseleave)="notShowSubMenu(item, i)"> <span>{{item.menuName}}</span> <dl *ngFor="let child of item.menuChildren, let k = index" [ngStyle]="{'display': item.showSubMenu ? 'block':'none','margin-left': '-2.6rem'}"> <dd>{{child.menuChidrenItem}}</dd> </dl> </li> </ul> </div> </div>
實際效果:
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Angular如何實現二級導航欄”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。