独立组件间共享Mixins

Mixins:用于组件间事件共享(公用的方法,比如弹窗,数值计算等组件函数

要使用Mixins,首先要在项目文件夹根目录下安装Mixins包

react>npm install --save  react-mixin@2     //指定安装版本

使用:

step-1  建立公共mixins.js文件,将公共函数放在里面

const MixinLog={
    log(){
       console.log('数据打印成功:123...')
    }
};

export default MixinLog 

step-2  安装react-mixin包(在ES6下使用,必须安装该插件)

step-3 在使用组件中引入react-mixin及mixins.js

import ReactMixin from 'react-mixin';

import MixinLog from './mixins'

step-4  在组件中引入mixins.js中定义的方法

ReactMixin(BodyIndex.prototype, MixinLog);

step-5  在函数中写入方法

changeLogin(name){ 

          MixinLog.log();

      }
原文地址:https://www.cnblogs.com/jeanneze/p/6857684.html