我需要在我的 Angular 9 應用程序中有一個介紹頁面。我有一個mat-button,當我單擊它時,我需要顯示主頁,其中將顯示主導航和其他內容。到目前為止我嘗試過的是在我的 app.component.html 中<app-main-nav *ngIf="!showActions"></app-main-nav><app-bgphotos ></app-bgphotos><router-outlet></router-outlet>在我的app.component.ts中public showActions: boolean;constructor() { }ngOnInit() { this.showActions = false; }public toggle(): void { this.showActions = !this.showActions; }在我的 homepage.component.html 中<div class="bgpageland"><div class="wlctext"><p>Welcome</p><p>serv</p><button type="button" (click)="toggle()">Intro</button>沒有任何反應,兩個組件都被顯示見下圖https://i.stack.imgur.com/mC5hS.jpg
2 回答

守著一只汪
TA貢獻1872條經驗 獲得超4個贊
實際上,您在“homepage.component.html”中綁定了單擊事件,并且單擊事件在“app.component.ts”中定義。如果您想從 clild 組件調用父組件中的函數,您應該使用 EventEmitter 并使用 @Output 綁定到“homepage.component.html”。

烙印99
TA貢獻1829條經驗 獲得超13個贊
正如您所說,您有一個墊按鈕,當您單擊它時,它應該顯示主頁
你可以使用角度路由器
第一次導入 Router
import { Router } from '@angular/router';
那么你的邏輯應該是
public showActions: boolean;
constructor( private router: Router ) { }
ngOnInit() { this.showActions = false; }
public toggle(): void {
this.showActions = !this.showActions;
this.router.navigate(['/home']);
}
- 2 回答
- 0 關注
- 134 瀏覽
添加回答
舉報
0/150
提交
取消