You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Angular.io(Angular 4+)应用中使用React组件?

Using React Components in an Angular.io App: Feasible, Production-Ready Solutions

Absolutely! You can totally integrate your enterprise React UI library into an Angular app—no need to rewrite everything from scratch. Here are two reliable approaches that work great for production:

1. Wrap React Components as Web Components

This is a framework-agnostic method that lets you reuse your React components across any modern frontend stack, including Angular. Here's how to implement it:

  • Use tools like react-to-webcomponent or @lit/react to wrap your existing React components into standard Web Components. These tools handle translating React's props and events to Web Component standards automatically.
  • Bundle the wrapped components (make sure React and ReactDOM are included in the bundle, or load them globally if your Angular app doesn’t already have them).
  • In your Angular app, add the Web Component bundle to your angular.json assets array, or import it directly in main.ts.
  • Enable custom elements in your Angular module by adding CUSTOM_ELEMENTS_SCHEMA to the module’s schemas array—this tells Angular to ignore unknown HTML tags (your Web Components) and avoid template errors.
  • Now you can use the Web Component just like any other HTML element in your Angular templates. For example:
    <react-enterprise-table [data]="tableData" (rowClick)="handleRowClick($event)"></react-enterprise-table>
    

2. Use the @angular-react/core Integration Library

This library is built specifically to bridge Angular and React, letting you use React components directly in Angular templates without manual Web Component wrapping. It handles prop binding, event sync, and lifecycle hooks out of the box. Here’s the workflow:

  • Install the required packages first:
    npm install @angular-react/core @angular-react/react react react-dom
    
  • Import ReactModule into your Angular app module (and any additional feature modules where you’ll use React components).
  • Create an Angular wrapper component for each React UI component using the @ReactComponent decorator. This connects the React component to Angular’s ecosystem:
    import { Component, Input, Output, EventEmitter } from '@angular/core';
    import { ReactComponent } from '@angular-react/core';
    import { EnterpriseDatePicker } from 'your-react-ui-library';
    
    @Component({
      selector: 'app-react-date-picker',
      template: `<EnterpriseDatePicker [props]="pickerProps"></EnterpriseDatePicker>`,
    })
    @ReactComponent({ component: EnterpriseDatePicker })
    export class ReactDatePickerComponent {
      @Input() selectedDate: Date;
      @Output() dateChanged = new EventEmitter<Date>();
    
      get pickerProps() {
        return {
          value: this.selectedDate,
          onChange: (newDate: Date) => {
            this.dateChanged.emit(newDate);
          },
        };
      }
    }
    
  • Now you can use <app-react-date-picker [selectedDate]="currentDate" (dateChanged)="updateDate($event)"></app-react-date-picker> in any Angular template just like a native Angular component.

Critical Things to Keep in Mind

  • Dependency Duplication: Ensure React and ReactDOM are only loaded once in your app. Check your package.json for duplicate versions—this can cause weird runtime bugs like unmounted component errors.
  • Performance: Both approaches have minimal overhead, but for large, complex component trees, do a quick performance test in your specific app context to ensure smooth rendering.
  • Type Safety: If your React library has TypeScript types, leverage them in your Angular wrappers to maintain type checking and avoid runtime errors.

内容的提问来源于stack exchange,提问作者dragonfly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:34:43