Vue中jsx的最簡單用法


最終頁面顯示效果為

<div class="open-service" style="color: #0199f0; cursor: pointer;">
    hello
    <p>world</p>
</div>

主頁面

parent.vue

<template>
  <div class="hello">
    <vue-test :typeSpan="typeSpan"></vue-test>
  </div>
</template>

<script type="text/jsx">
  import VueTest from "./jsx/VueTest";
export default {
  name: 'HelloWorld',
  components:{
    VueTest,
  },
  computed:{
    typeSpan(){
      return {
        text:'hello',
        attrs:{
          class:"open-service",
          style:'color:#0199f0;cursor:pointer'
        },

      }
    }
  },
}
</script>

子頁面child.vue有兩種方法

第一種

<script type="text/jsx">
export default {
    props: {
        typeSpan:{
            type:Object,
            default:{}
        }
    },
    created(){

    },
    methods:{
        openContactService(){
            alert(1)
        },
    },
    render(createElement) {
        return(
            <div
                class={this.typeSpan.attrs.class}
                style={this.typeSpan.attrs.style}
                onClick={this.openContactService}>
                {this.typeSpan.text}
              <p >world</p>
            </div>
        )
    }
}
</script>

第二種

<script type="text/jsx">
export default  {
    props:{
        typeSpan:{
            type:Object,
            default:{}
        }
    },
    methods:{
        openContactService(){
          alert(1)
      }
    },
   render(createElement, context) {
        return createElement(
            'div',{
                class:this.typeSpan.attrs.class,
                style:this.typeSpan.attrs.style,
                on:{
                    click:this.openContactService
                }
            },[
                this.typeSpan.text,
                createElement('p','world'),
            ]

        )
   }
}
</script>

這是兩個最簡單的例子

參考鏈接 https://cn.vuejs.org/v2/guide/render-function.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM