我需要在我的函数中传递多个元素作为参数,这里我只使用1个元素
var first = require('./First’)
如何在这里传递更多元素作为参数?例如,还有
var second = require(‘./Second’)
这里是代码示例
var fs = require('fs')
var React = require('react')
var ReactDOMServer = require('react-dom/server')
var First = require('./First')
var Second = require('./Second')
var build = function(name, props) {
var svg = ReactDOMServer.renderToStaticMarkup(React.createElement(First, props))
fs.writeFileSync('client/data/' + name + '.svg', svg)
}
build(‘my-icon', {})
答案 0 :(得分:1)
<强> ES5 强>
var fs = require('fs');
var React = require('react');
var ReactDOMServer = require('react-dom/server');
var Manna = require('./Manna');
var Cog = require('./Cog');
var build = function(name, props) {
var size = props.size || 64;
var svgProps={
xmlns:"http://www.w3.org/2000/svg",
viewBox:[0, 0, size, size].join(' '),
width:size,
height:size
};
var svg = ReactDOMServer.renderToStaticMarkup(
React.createElement("svg",svgProps,
React.createElement(Cog,props),
React.createElement(Manna,props))
);
fs.writeFileSync('client/data/' + name + '.svg', svg);
};
build('manna-icon', {});
ES6 JSX和BABEL
import fs from 'fs';
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import Manna from './Manna';
import Cog from './Manna';
const build = (name="defaultName", props)=> {
let size = props.size || 64;
let svgProps={
xmlns:"http://www.w3.org/2000/svg",
viewBox:`0 0 ${size} ${size}`,
width:size,
height:size
};
let svg = renderToStaticMarkup(
<svg {...svgProps}>
<Cog {...props}/>
<Manna {...props}/>
</svg>);
fs.writeFileSync(`client/data/${name}.svg`, svg);
};
build('manna-icon', {});
对于实例babel-cli
所需的jsx和es6功能npm install -D babel-cli nodemon
然后在package.json
{...
"scripts":{
"start":"nodemon --exec babel-node server.js"
}
...
}