Props

  1. import { WeElement, define, render } from 'omi'
  2. define('my-first-element', class extends WeElement {
  3. render(props) {
  4. return (
  5. <h1>Hello, {props.name}!</h1>
  6. )
  7. }
  8. })
  9. render(<my-first-element name="world"></my-first-element>, 'body')

你也可以传任意类型的数据给 props:

  1. import { WeElement, define, render } from 'omi'
  2. define('my-first-element', class extends WeElement {
  3. render(props) {
  4. return (
  5. <h1>Hello, {props.myObj.name}!</h1>
  6. )
  7. }
  8. })
  9. render(<my-first-element my-obj={{ name: 'world' }}></my-first-element>, 'body')

my-obj 将映射到 myObj,驼峰的方式。你可以通过静态属性 props 来设置默认值:

  1. import { WeElement, define, render } from 'omi'
  2. define('my-first-element', class extends WeElement {
  3. static defaultProps = {
  4. name: 'Omi',
  5. myAge: 18
  6. }
  7. render(props) {
  8. return (
  9. <h1>Hello, {props.name}! Age {props.myAge}</h1>
  10. )
  11. }
  12. })
  13. render(<my-first-element name="world"></my-first-element>, 'body')

通过 props ,你可以透传 style 或者 class 给根节点,比如 → 这里 透传 style:

  1. <el-button onClick={this.onClick} style="color:red;">默认按钮1</el-button>
  2. <el-button type="primary" style={{color:'red'}}>主要按钮</el-button>