CSS

  1. define('my-first-element', class extends WeElement {
  2. css() {
  3. return `h1 { color: red; }`
  4. }
  5. render(props) {
  6. return (
  7. <h1>Hello, world!</h1>
  8. )
  9. }
  10. })
  11. render(<my-first-element onMyEvent={(evt) => { alert(evt.detail.name) }}></my-first-element>, 'body')

你也可以在 JS 里动态拼接 CSS:

  1. css() {
  2. return `h1 { color: ${Math.random() > 0.5 ? "red" : "blue"}; }`
  3. }

你也可以另起一个文件用来写 CSS,但是需要配置一下 webpack to-string-loader

  1. {
  2. test: /[\\|\/]_[\S]*\.scss$/,
  3. use: [
  4. 'to-string-loader',
  5. 'css-loader',
  6. 'sass-loader'
  7. ]
  8. }

然后:

  1. import { define, WeElement } from 'omi'
  2. import style from '../style/_button.scss'
  3. define('el-button', class extends WeElement {
  4. static pure = true
  5. css() {
  6. return style
  7. }
  8. ...
  9. ...