创建 React 应用创建 React 应用的最简单的方法是使用 [color=var(--vscode-textLink-foreground)]创建 React App, 如使用npx 包运行工具: - npx create-react-app arjs-designer-app
复制代码创建 React应用更多可参考文档 [color=var(--vscode-textLink-foreground)]官方文档 安装 ActivereportsJS- npm install @grapecity/activereports@beta
复制代码也可以使用 yarn命令: yarn add @grapecity/activereports添加设计器宿主元素在src 文件夹中添加 DesignerHost.js (或如果您使用的是 typescript DesignerHost.ts) 文件。 添加需要的 import 包DesignerHost.js(ts) - import React from "react";
- import { Designer as ReportDesigner } from "@grapecity/activereports/reportdesigner";
- import "@grapecity/activereports/styles/ar-js-ui.css";
- import "@grapecity/activereports/styles/ar-js-designer.css";
复制代码添加以下函数来初始化设计器,改函数是用于接收 宿主元素的选择器:
- const initDesigner = (designerHostSelector) => {
- new ReportDesigner(designerHostSelector);
- };
复制代码添加功能模块 DesignerHost.js(ts) - export const DesignerHost = () => {
- React.useEffect(() => initDesigner("#designer-host"), []);
- return <div id="designer-host"></div>;
- };
复制代码该组件调用了 initDesigner 函数,一旦组件完成渲染会,将设计器添加到#designer-host 元素。 在 index.css 文件为 designer-host 元素添加样式文件。 - #designer-host {
- margin: 0 auto;
- width: 100%;
- height: 100vh;
- }
复制代码 在应用中添加设计器组件打开App.js(ts)文件使用以下代码替换默认代码: - import React from "react";
- import "./App.css";
- import { DesignerHost } from "./DesignerHost";
- function App() {
- return <DesignerHost />;
- }
- export default App;
复制代码 运行并测试应用使用npm start 或 yarn start 命令运行应用,可能会存在错误:“fail with the fatal error that the JavaScript heap is out of memory”,打开package.json文件替换start脚本: react-scripts --max_old_space_size=4096 start 重新运行 npm start 或 yarn start 命令。
|