1. 程式人生 > >獨立組件間共享Mixins

獨立組件間共享Mixins

.proto 成功 數據 install mixins 文件 name 打印 div

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();

      }

獨立組件間共享Mixins