如何从 angular 调用自定义组件 9

How to call the custom component from angular 9

我正在尝试构建一个 Angular 9 项目,我是初学者,我正在尝试一个非常简单的示例,方法是创建一个包含 .ts、.html 文件和我试图在 运行 时绑定这些文件。我创建了

test.component.ts
test.component.html

我已将组件包含在 NgModule 中

下面是我托管项目的 link。请帮助,我是这项技术的新手。

Project link

这取决于你的组件之间的关系。

如果它们是不相关的组件,您可以使用 routing Demo

您需要将路由模块导入应用模块

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"},

如果它们是 parent/child 那么你可以使用

<test></test>

在您的演示 hello 组件中就是一个例子。