简体   繁体   English

如何将反应自定义包用于角度指令

[英]How to use react custom package into an angular directive

Hi i have an angular v1.x application and a react custom package.嗨,我有一个 angular v1.x 应用程序和一个 react 自定义包。


I want to be able to use the react custom package into the angular directive, which eventually will display a react component.我希望能够将 react 自定义包使用到 angular 指令中,最终将显示一个 react 组件。

some code:一些代码:


  1. angular app:角度应用:

directive that consumes the my-package使用my-package指令

import app from '../../main'
import React from "react";
import ReactDOM from "react-dom";
import Mypackage from 'my-package';

const reactDirective = app.directive('injectReact', function() {
 return {
  template: '<div id="reactapp" class="react-part"></div>',
  scope: {},
  link: function(scope, el, attrs){
        scope.newItem = (value) => {alert (value)}

        const reactapp = document.getElementById('reactapp')
        scope.$watch('scopeval', function(newValue, oldValue) {
            if (angular.isDefined(newValue)) {
             ReactDOM.render(
                 <div>
                    <Mypackage />
                 </div>
                , reactapp);
           }
        }, true);
     }
  }
})
  1. The package is linked into the project, so 'my-package' is inside angular's node_modules folder.该包已链接到项目中,因此“my-package”位于 angular 的node_modules文件夹中。

  2. The application uses bower & gruntfile.js to gother all the js files of the application应用程序使用bower & gruntfile.js来收集应用程序的所有 js 文件

  3. i cant use import or require into the directive file.我不能在指令文件中使用importrequire

  4. so i miss some babel / browserify configuration to make this work.????所以我想念一些babel / browserify配置来完成这项工作。????

Any help is welcome.欢迎任何帮助。

The easiest way you can achieve this is by using external package like react2angular .最简单的方法是使用像react2angular这样的外部包。 This package allows you to create angular.js wrapper components which will render specific react components, allowing you to pass props to them.这个包允许你创建 angular.js 包装器组件,它将呈现特定的反应组件,允许你将道具传递给它们。 Linking process is quite simple:链接过程非常简单:

angular
  .module('myModule', [])
  .component('myComponent', react2angular(MyComponent, [], ['$http', 'FOO']))

If you want to create you own wrapping library checkout how it's made here (rendering, passing props and watching for changes to them)如果您想创建自己的包装库,请查看它的制作方式(渲染、传递道具并观察对它们的更改)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM