默认的模板方式,和vue2差不多,在组件中使用setup函数

// 父组件
<template>
  <div>
    <div>
      <div>{{city}}</div>
      <button @click="changereactive">改变reactive</button>
      <button @click="handlefather">点击父组件</button>
    </div>
    <child ref="childref" @handlebtn="handlebtn" @testclick="testclick" city="成都" />
  </div>
</template>

<script>
import { ref, onmounted, torefs, reactive } from 'vue'
import child from './child.vue'

export default {
  components: {
    child
  },
  setup () {
    const handlebtn = (val) => {
      console.log('btn', val)
    }

    const testclick = (val) => {
      console.log('testclick', val)
    }

    const childref = ref(null)

    const handlefather = () => {
      childref.value.observed.a = 666 //父组件修改子组件的值
      console.log('获取子组件的方法', childref.value)
      // 子组件需要定义expose,如果不定义,那么需要返回,相应的函数,一般不直接返回,如果页面上没有用到
      //直接通过expose(暴露)需要的方法或者值就行了
    }

    // 通过setup函数的方法写,需要返回,页面上用到的方法,和值
    // 如果是reactve定义的值,通过解构的方式页面上渲染的值不是响应式的,需要通过torefs转换,然后解构
    // ...torefs(testreactive)
    
    const testreactive = reactive({
      city: '北京',
      age: 22
    })

    const changereactive = () => {
      testreactive.city = '重庆'
    }

    return {
      handlebtn,
      testclick,
      handlefather,
      ...torefs(testreactive),
      changereactive,
      childref
    }
  }
}
</script>


//子组件
<template>
  <div>
    {{observed.a}}
    <button @click="handlebtn">点击</button>
  </div>
</template>

<script>
import { defineprops, defineemits, defineemit, defineexpose, reactive } from 'vue'

export default {
  props: {
    city: string
  },

  /* 设置这个主要是为了,让ctx.attrs访问不到这个属性 */
  /* props上设置了有的属性,在attrs上,也不会显示 */

  emits: ['testclick'],  //设置这个的目的,是为了让attrs上没有对应的自定义方法,
  //子组件如果设置了peops,那么在attrs上也访问不到对应的值
  //arrts在vue3中功能有所增强,挂载了自定义方法,和class,style
  //在vue2中自定义方法是挂载到,$listeners,在vue3中$liseners已被移除

  setup (props, ctx) {
    const { expose, emit } = ctx
    const handlebtn = () => {
      console.log('btn', ctx)
      emit('testclick', 666)
    }

    const observed = reactive({
      a: 1
    })

    function clickchild (value) {
      observed.a = value
    }

    expose({
      clickchild, //暴露自定义方法,父组件调用
      observed// 暴露子组件的值
    })

    return {
      observed,
      handlebtn
    }
  }
}
</script>

在script标签上写setup  <script setup>

// 父组件
<template>
  <div>
    <button @click="handlefather">点击父</button>
    <child ref="childref" @handlebtn="handlebtn" @testclick="testclick" city="成都" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import child from './child.vue'
// 这种方式写不用在return导出页面上用到的方法和值,需要用什么直接在vue上解构出对应的defin
const childref = ref(null)

const handlebtn = (val) => {
  console.log('btn', val)
}

const testclick = (val) => {
  console.log('testclick', val)
}

const handlefather = () => {
  console.log('获取子组件的方法', childref.value)
  childref.value.testfatherclick()  //父组件调用子组件的方法
  // 子组件通过defineexpose暴露出对应的方法
}

</script>


// 子组件
<template>
  <div>
    <button @click="handlebtn">点击</button>
  </div>
</template>

<script setup>
import { defineprops, defineemits, defineexpose, reactive } from 'vue'

const props = defineprops({
  city: string
})

const emit = defineemits(['handlebtn', 'testclick'])

const handlebtn = () => {
  // console.log('btn', props, emit)
  emit('testclick', 12)
}

const testfatherclick = () => {
  console.log('测试父组件点击子组件')
}

const observed = reactive({
  a: 1
})

defineexpose({ //暴露方法给父组价
  testfatherclick,
  observed
})

</script>

<style scoped>
</style>

通过jsx方式渲染,非常接近react的方式,也是我最推荐的方式,jsx对ts也很支持,.vue文件没有tsx支持得好

// 父组件
import { ref, reactive } from 'vue'
import child from './child.jsx'

const father = {
  setup() {
    // 在jsx中定义的ref在页面上使用需要通过.value去访问
    const city = ref('北京')

    const changecity = () => {
      city.value = '杭州'
    }

    const childref = ref(null)

    const handelfather = (add) => {
      //也是通过在组件暴露expose方法
      // city.value = '杭州'
      console.log('childref', childref.value)
    }

    const testchildclick = (val) => {
      console.log('测试子组件点击', val)
    }

    return () => {
      return (
        <div>
          <div>{city.value}</div>
          <button onclick={changecity}>改变城市</button>
          <button onclick={handelfather}>点击父</button>
          <child testchildclick={testchildclick} ref={childref} />
        </div>
      )
    }
  }
}

export default father


//子组件
import { ref, reactive } from 'vue'

const child = {
  props: {
    testchildclick: function
  },

  setup(props, { emit, expose }) {
    const { testchildclick } = props
    const testfatherclick = () => {
      console.log('测试父组件点击子组件')
    }

    const handelbtn = () => {
      // emit('testchildclick') //在jsx中这种方式不行
      // console.log('props', props)
      testchildclick('返回值给父组件')
      // 只能通过这种方法,这也相当于react,相当于传递一个函数给子组件,子组件把值,通过函数传给父组件
    }

    expose({
      testfatherclick
    })

    return () => {
      return (
        <div>
          <button onclick={handelbtn}>子组件传值给父组件</button>
        </div>
      )
    }
  }
}

export default child

总结

到此这篇关于vue3不同语法格式对比的文章就介绍到这了,更多相关vue3语法格式对比内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!