简体   繁体   English

通过cosmos在Meteor中使用npm包:browserify

[英]Using npm package in Meteor via cosmos:browserify

I'm trying to load Radium (which is a javascript library for inline css) following instructions here . 我正在尝试按照此处的说明加载Radium(这是一个内联css的javascript库)。

In app.browserify.js : Radium = require("radium"); app.browserify.jsRadium = require("radium"); .

In package.json : "radium": "0.13.4" package.json"radium": "0.13.4"

However when I try to use Radium in js in the app, the inline css doesn't work. 但是,当我尝试在应用程序中的js中使用Radium时,内联css不起作用。 Chrome dev tool indicates that Radium = module.exports(ComposedComponent). Chrome开发工具表示Radium = module.exports(ComposedComponent). .

I'm assuming this should be an object, considering that ReactPIXI that I loaded the same way, works just fine, and the dev tool says ReactPIXI = Object {factories: Object} . 我假设这应该是一个对象,考虑到我以相同的方式加载的ReactPIXI,工作正常,开发工具说ReactPIXI = Object {factories: Object}

Here is my code: 这是我的代码:

AppBody = React.createClass({
  mixins: [ReactMeteorData, Navigation, State, Radium.StyleResolverMixin,   
  Radium.BrowserStateMixin],

render: function() {
  var self = this;
  var styles = {
    base: {
      color: this.state.fontColor,
      background: 'red',
    states: [
      {hover: {background: 'blue', color: 'red'}},
      {focus: {background: 'pink', outline: 'none', color: 'yellow'}}
    ]

    //also tried
    //':hover': {background: 'blue', color: 'red'},
    //':focus': {background: 'pink', outline: 'none', color: 'yellow'}
  },
  primary: {
    background: 'green'
  },
  warning: {
    background: 'purple'
  }
};


var items = this.state.items.map(function(item, i) {
  return (
      <div>
        <div style= {[styles.base, styles['warning']]} key={item}>
      // also tried <div style = {this.buildStyles(styles)} key={item}>
          {item}
        </div>
        <button style = {[styles.base, styles['warning']]} onClick={update} >Remove</button>
      </div>
  );
}.bind(this));
return (
       {items}
     )

The issue was resolved by wrapping the React.createComponent with Radium as instructed in the Radium documentation. 通过Radium文档中的指示将React.createComponent与Radium包装在一起解决了该问题。 Instead of using the mixins, the code now looks like this and it works as intended. 代码现在看起来像这样,而不是使用mixins,它按预期工作。

AppBody = Radium(React.createClass({
  mixins: [ReactMeteorData, Navigation, State],

render: function() {
  var self = this;
  var styles = {
    base: {
      color: this.state.fontColor,
      background: 'red',
    ':hover': {background: 'blue', color: 'red'},
    ':focus': {background: 'pink', outline: 'none', color: 'yellow'}
  },
  primary: {
    background: 'green'
  },
  warning: {
    background: 'purple'
  }
};


var items = this.state.items.map(function(item, i) {
  return (
  <div>
    <div style= {[styles.base, styles['warning']]} key={item}>
      {item}
    </div>
    <button style = {[styles.base, styles['warning']]} onClick={update} >Remove</button>
  </div>
);
}.bind(this));
  return (
        {items}
      )
)}));

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

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