簡體   English   中英

如何從 angular 9 調用自定義組件

[英]How to call the custom component from angular 9

我正在嘗試構建一個 Angular 9 項目並且我是初學者,我正在嘗試一個非常簡單的示例,方法是創建一個包含 a.ts、.html 文件的自定義組件,並且我正在嘗試在運行時綁定這些文件。 我創造了

test.component.ts
test.component.html

我已將組件包含在 NgModule 中

下面是我主持該項目的鏈接。 請幫忙,我是這項技術的新手。

項目鏈接

這取決於組件之間的關系。

如果它們是不相關的組件,您可以使用路由演示

您需要將路由模塊導入應用程序模塊

imports:[ BrowserModule, FormsModule,
        RouterModule.forRoot([
            { path: 'test', component: testComponent }
          ]) 
  ]

然后把<router-outlet ></router-outlet>放到 app.component.html 這將路由標簽使用它。

你可以像<a routerLink="/test">Test</a>一樣路由

如果您想將其設為默認頁面,請將其放在 app.module 中的路由中

 {path:"",redirectTo:"test",pathMatch:"full"},

如果他們是父母/孩子,那么你可以使用

<test></test>

在您的演示 hello 組件中就是一個例子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM