最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3不同語(yǔ)法格式對(duì)比的實(shí)例代碼

 更新時(shí)間:2021年08月10日 09:19:55   作者:artist  
vue3發(fā)布已有一段時(shí)間了,文檔也大概看了一下,不過(guò)對(duì)于學(xué)一門技術(shù),最好的方法還是實(shí)戰(zhàn),這篇文章主要給大家介紹了關(guān)于vue3不同語(yǔ)法格式對(duì)比的相關(guān)資料,需要的朋友可以參考下

默認(rèn)的模板方式,和vue2差不多,在組件中使用setup函數(shù)

// 父組件
<template>
  <div>
    <div>
      <div>{{city}}</div>
      <button @click="changeReactive">改變r(jià)eactive</button>
      <button @click="handleFather">點(diǎn)擊父組件</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,如果不定義,那么需要返回,相應(yīng)的函數(shù),一般不直接返回,如果頁(yè)面上沒(méi)有用到
      //直接通過(guò)expose(暴露)需要的方法或者值就行了
    }

    // 通過(guò)setup函數(shù)的方法寫,需要返回,頁(yè)面上用到的方法,和值
    // 如果是reactve定義的值,通過(guò)解構(gòu)的方式頁(yè)面上渲染的值不是響應(yīng)式的,需要通過(guò)toRefs轉(zhuǎn)換,然后解構(gòu)
    // ...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">點(diǎn)擊</button>
  </div>
</template>

<script>
import { defineProps, defineEmits, defineEmit, defineExpose, reactive } from 'vue'

export default {
  props: {
    city: String
  },

  /* 設(shè)置這個(gè)主要是為了,讓ctx.attrs訪問(wèn)不到這個(gè)屬性 */
  /* props上設(shè)置了有的屬性,在attrs上,也不會(huì)顯示 */

  emits: ['testClick'],  //設(shè)置這個(gè)的目的,是為了讓attrs上沒(méi)有對(duì)應(yīng)的自定義方法,
  //子組件如果設(shè)置了peops,那么在attrs上也訪問(wèn)不到對(duì)應(yīng)的值
  //arrts在vue3中功能有所增強(qiáng),掛載了自定義方法,和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, //暴露自定義方法,父組件調(diào)用
      observed// 暴露子組件的值
    })

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

在script標(biāo)簽上寫setup  <script setup>

// 父組件
<template>
  <div>
    <button @click="handleFather">點(diǎn)擊父</button>
    <Child ref="childRef" @handleBtn="handleBtn" @testClick="testClick" city="成都" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import Child from './Child.vue'
// 這種方式寫不用在return導(dǎo)出頁(yè)面上用到的方法和值,需要用什么直接在vue上解構(gòu)出對(duì)應(yīng)的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()  //父組件調(diào)用子組件的方法
  // 子組件通過(guò)defineExpose暴露出對(duì)應(yīng)的方法
}

</script>


// 子組件
<template>
  <div>
    <button @click="handleBtn">點(diǎn)擊</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('測(cè)試父組件點(diǎn)擊子組件')
}

const observed = reactive({
  a: 1
})

defineExpose({ //暴露方法給父組價(jià)
  testFatherClick,
  observed
})

</script>

<style scoped>
</style>

通過(guò)jsx方式渲染,非常接近react的方式,也是我最推薦的方式,jsx對(duì)ts也很支持,.vue文件沒(méi)有tsx支持得好

// 父組件
import { ref, reactive } from 'vue'
import Child from './Child.jsx'

const Father = {
  setup() {
    // 在jsx中定義的ref在頁(yè)面上使用需要通過(guò).value去訪問(wèn)
    const city = ref('北京')

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

    const childRef = ref(null)

    const handelFather = (add) => {
      //也是通過(guò)在組件暴露expose方法
      // city.value = '杭州'
      console.log('childRef', childRef.value)
    }

    const testChildClick = (val) => {
      console.log('測(cè)試子組件點(diǎn)擊', val)
    }

    return () => {
      return (
        <div>
          <div>{city.value}</div>
          <button onClick={changeCity}>改變城市</button>
          <button onClick={handelFather}>點(diǎn)擊父</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('測(cè)試父組件點(diǎn)擊子組件')
    }

    const handelBtn = () => {
      // emit('testChildClick') //在jsx中這種方式不行
      // console.log('props', props)
      testChildClick('返回值給父組件')
      // 只能通過(guò)這種方法,這也相當(dāng)于react,相當(dāng)于傳遞一個(gè)函數(shù)給子組件,子組件把值,通過(guò)函數(shù)傳給父組件
    }

    expose({
      testFatherClick
    })

    return () => {
      return (
        <div>
          <button onClick={handelBtn}>子組件傳值給父組件</button>
        </div>
      )
    }
  }
}

export default Child

總結(jié)

到此這篇關(guān)于vue3不同語(yǔ)法格式對(duì)比的文章就介紹到這了,更多相關(guān)vue3語(yǔ)法格式對(duì)比內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 簡(jiǎn)單方法實(shí)現(xiàn)Vue?無(wú)限滾動(dòng)組件示例

    簡(jiǎn)單方法實(shí)現(xiàn)Vue?無(wú)限滾動(dòng)組件示例

    這篇文章主要為大家介紹了簡(jiǎn)單方法實(shí)現(xiàn)Vue?無(wú)限滾動(dòng)組件示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • 深入理解vue.js中的v-if和v-show

    深入理解vue.js中的v-if和v-show

    這篇文章主要給大家深入的介紹了關(guān)于vue.js中v-if和v-show的相關(guān)資料,文中詳細(xì)介紹兩者的共同點(diǎn)和區(qū)別,通過(guò)圖文介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-06-06
  • vue導(dǎo)出word純前端的實(shí)現(xiàn)方式

    vue導(dǎo)出word純前端的實(shí)現(xiàn)方式

    這篇文章主要介紹了vue導(dǎo)出word純前端的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue中的組件及路由使用實(shí)例代碼詳解

    Vue中的組件及路由使用實(shí)例代碼詳解

    組件系統(tǒng)是 Vue 的一個(gè)重要概念,因?yàn)樗且环N抽象,允許我們使用小型、獨(dú)立和通??蓮?fù)用的組件構(gòu)建大型應(yīng)用。這篇文章主要介紹了Vue中的組件及路由使用 ,需要的朋友可以參考下
    2019-05-05
  • vue-cli 3如何使用vue-bootstrap-datetimepicker日期插件

    vue-cli 3如何使用vue-bootstrap-datetimepicker日期插件

    這篇文章主要介紹了vue-cli 3如何使用vue-bootstrap-datetimepicker日期插件,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下
    2021-02-02
  • vue 之 css module的使用方法

    vue 之 css module的使用方法

    這篇文章主要介紹了vue 之 css module的使用方法,css module目的為所有類名重新生成類名,有效避開(kāi)了css權(quán)重和類名重復(fù)的問(wèn)題,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-12-12
  • vue基于Element按鈕權(quán)限實(shí)現(xiàn)方案

    vue基于Element按鈕權(quán)限實(shí)現(xiàn)方案

    這篇文章主要介紹了vue基于Element按鈕權(quán)限實(shí)現(xiàn)方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Next.js路由布局機(jī)制詳解

    Next.js路由布局機(jī)制詳解

    本文主要介紹了Next.js路由布局機(jī)制詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • vue如何根據(jù)數(shù)值判斷顏色

    vue如何根據(jù)數(shù)值判斷顏色

    這篇文章主要介紹了vue如何根據(jù)數(shù)值判斷顏色問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue3中如何實(shí)現(xiàn)定義全局變量

    vue3中如何實(shí)現(xiàn)定義全局變量

    這篇文章主要介紹了vue3中如何實(shí)現(xiàn)定義全局變量,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07

最新評(píng)論

军事| 廉江市| 尼勒克县| 兴业县| 吉木萨尔县| 清涧县| 孙吴县| 措美县| 东乡| 张家港市| 海门市| 长治市| 田东县| 台湾省| 上栗县| 镇原县| 洪洞县| 塘沽区| 右玉县| 正镶白旗| 双桥区| 贵定县| 金昌市| 安西县| 昔阳县| 临澧县| 图片| 克拉玛依市| 密云县| 江门市| 东城区| 涪陵区| 清远市| 长沙县| 聊城市| 车致| 商城县| 诸暨市| 墨脱县| 济南市| 赤水市|