JavaScript/vue.js/$createElement

来自康健生活
Kangkk​(留言 | 贡献)2020年10月21日 (三) 10:16的版本 (创建页面,内容为“== 前言 == == 示例 == <syntaxhighlight lang="javascript"> this.$createElement({ template: ` <div @click="clickHandler">…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

前言

示例

this.$createElement({
          template: `
                <div @click="clickHandler">
                    <h1>This component is created by render function :):):)</h1>
                    <p>
                        {{statusNum}}
                    </p>
                    <h2>My name is: {{name}}</h2>
                    <h2>My age is: {{age}}</h2>
                    <slot></slot>
                </div>
            `,
          props: ['testProp1', 'testProp2'],
          data: function () {
            return {
              statusNum: Math.random() * 10000
            }
          },
          methods: {
            clickHandler: function () {
              this.$data.statusNum = Math.random() * 1000
            }
          }
        }, {
          // 和`v-bind:class`一样的 API
          'class': {
            foo: true,
            bar: false
          },
          // 和`v-bind:style`一样的 API
          style: {
            color: 'red',
            fontSize: '14px'
          },
          // 正常的 HTML 特性
          attrs: {
            id: 'foo'
          },
          // 组件 props
          props: {
            myProp: 'bar'
          }
        }, [
          '先写一些文字',
          this.$createElement('h1', '一则头条')
        ])

(来源摘自互联网示例,作者 )