angular组件封装

本文介绍了如何在Angular中进行组件封装,从创建公共组件到在实际项目中使用,包括编写c-dropdown.component.ts、c-dropdown.component.html及c-dropdown.module.ts等文件,并在页面中引入和解决在父级路由中引入组件时遇到的问题,最终实现组件封装的成功应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.这个公共组件的封装

2.c-dropdown.component.ts

import { Component, OnInit, Input,  EventEmitter, Output, ViewChild } from '@angular/core';

@Component({
  selector: 'c-dropdown',
  templateUrl: './c-dropdown.component.html',
  styleUrls: ['./c-dropdown.component.scss']
})
export class CDropdownComponent implements OnInit {
  ngOnInit() {
    // this.totalPage = Math.ceil(this.total / this.pageSize);
  }
}

3.c-dropdown.component.html

<div>
  <h3>this is the component</h3>
</div>

4.c-dropdown.component.scss(空)

5.c-dropdown.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { CDropdownComponent } from './c-dropdown.component';


@NgModule({
  imports: [
    CommonModule,
    FormsModule,
  ],
  declarations: [
    CDropdownComponent
  ],
  exports: [
    CDropdownComponent
  ]
})
export class CDropdownModule { }

6.在需要的页面中引入封装组件。

7.在页面中引入封装的组件

8.报错,说没有c-dropdown这个元素。因为这个页面是父级的子页面,所以还要在其父级的路由中引入改封装的组件。

9封装组件成功。

.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值