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

簡(jiǎn)單聊聊vue3.0 sfc中setup的變化

 更新時(shí)間:2021年10月09日 09:08:22   作者:new_cheng  
這篇文章主要給大家介紹了關(guān)于vue3.0 sfc中setup變化的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

前言

在vue中,sfc(單文件組件)指的是文件后綴名為.vue的特殊文件格式,它允許將 Vue 組件中的模板、邏輯 與 樣式封裝在單個(gè)文件中。

以下是一個(gè)基本的sfc

<script>
export default {
  data() {
    return {
      greeting: 'Hello World!'
    }
  }
}
</script>

<template>
  <p class="greeting">{{ greeting }}</p>
</template>

<style>
.greeting {
  color: red;
  font-weight: bold;
}
</style>

vue3.0在最新的sfc提案中推出了setup的寫(xiě)法,下面讓我們來(lái)看看,新的提案都有哪些變化。

標(biāo)準(zhǔn)的sfc寫(xiě)法

在使用TS的情況下,標(biāo)準(zhǔn)的sfc需要借助defineComponent來(lái)進(jìn)行類型推斷。

<script lang="ts">
  import { defineComponent } from 'vue'
  
  export default defineComponent({
    setup() {
      return {
        // 暴露給template的屬性
      }
    }
  })
</script>

script-setup

script setup的推出,目的是為了讓開(kāi)發(fā)者更高效率的開(kāi)發(fā)組件,減少樣板內(nèi)容,減輕開(kāi)發(fā)負(fù)擔(dān)。僅僅需要給script標(biāo)簽添加一個(gè)setup屬性,就能將script變成setup函數(shù),同時(shí)定義的變量,函數(shù),導(dǎo)入的組件都會(huì)默認(rèn)暴露給模板。

變量暴露

標(biāo)準(zhǔn)的寫(xiě)法

<script>
import { defineComponent, ref} from 'vue'

export default defineComponent({
  setup() {
    const count = ref(0)
    return {
      count
    }
  }
})
</script>

<template>
  <div>
    parent{{count}}
  </div>
</template>

setup 寫(xiě)法

<script setup lang="ts">
import {ref} from 'vue'
  
const count = ref(0)
</script>

<template>
  <div>
    parent{{count}}
  </div>
</template>

組件掛載

標(biāo)準(zhǔn)的寫(xiě)法

<script lang="ts">
import { defineComponent } from 'vue'
import child from './childComponent'

export default defineComponent({
  components: {
    child
  },
  setup() {
    // ...
  }
})
</script>

<template>
  <div>
    parent
    <child />
  </div>
</template>

setup 寫(xiě)法

<script setup lang="ts">
import child from './childComponent'
</script>

<template>
  <div>
    parent
    <child />
  </div>
</template>

無(wú)需再手動(dòng)掛載組件,即可在模板中使用,高效快捷。 其他的變量,以及頂級(jí)API,比如compute、watch等屬性,和原來(lái)的標(biāo)準(zhǔn)寫(xiě)法一樣。

props

在setup中,子組件在接收props時(shí),需要借助defineProps,這是一個(gè)只能在setup語(yǔ)法中才能使用的API。我們先來(lái)看看標(biāo)準(zhǔn)的寫(xiě)法,props是如何接收的。

標(biāo)準(zhǔn)寫(xiě)法

// parent.vue
<template>
  <child :count={count} />
</template>
<script lang="ts">
impor {defineComponent,ref} from 'vue'
import child from './childComponent'

export default defineComponent({
  components: {
    child
  },
  setup() {
    const count = ref(0)
    return {
      count
    }
  }
})
</script>
// child.vue
<template>
  child{{count}}
</template>
<script lang="ts">
impor {defineComponent} from 'vue'
export default defineComponent({
  props: ['count'],
  setup(props) {
    return {}
  }
})
</script>

setup 寫(xiě)法,使用defineProps

// parent.vue
<template>
  <child :count={count} />
</template>
<script setup lang="ts">
impor {ref} from 'vue'
import child from './childComponent'
  
const count = ref<number>(0)
</script>
// child.vue
<template>
  child{{count}}
</template>
<script setup>
defineProps(['count'])
</script>


注意:使用sfc-setup語(yǔ)法,不需要引入defineProps

在這里我們只需要簡(jiǎn)單的聲明props,寫(xiě)法簡(jiǎn)潔了不少。


那如何給props做類型檢查呢?

<script setup>
defineProps({
  count: Number,
  title: {
    type: String,
    default: 'header'
  },
  data: {
    type: Object,
    defualt () {
      return {}
    }
  }
})
</script>


如何使用TS進(jìn)行類型注解呢?

<script lang="ts" setup>
interface d {
  name: string  
}
  
defineProps<{
  count: number // Number要換成ts的語(yǔ)法
  title: string
  data: d
}>()
</script>


我們發(fā)現(xiàn),props沒(méi)有被賦予默認(rèn)值,在TS的寫(xiě)法中,給props設(shè)置默認(rèn)值有2種方式

ES6的變量解構(gòu)賦值

defineProps返回一個(gè)對(duì)象,我們可以在解構(gòu)返回的對(duì)象,同時(shí)賦予默認(rèn)值。

<script lang="ts" setup>
interface d {
  name: string  
}
  
const {count = 0, title = 'header', date = { name: 'a' }} = defineProps<{
  count: number // Number要換成ts的語(yǔ)法
  title: string
  data: d
}>()
</script>

withDefaults


官方后續(xù)推出了withDefaults來(lái)給props提供默認(rèn)值;withDefaults會(huì)對(duì)默認(rèn)值進(jìn)行類型檢查。

<script lang="ts">
// 別忘記了引入 withDefaults
impor { withDefaults } from 'vue'
  
interface d {
  name: string  
}
  
const props = withDefaults(defineProps<{
  count: number // Number要換成ts的語(yǔ)法
  title: string
  data: d
}>(), {
  count: 0,
  title: 'header',
  data: () => ({name: '王小二'})
})
</script>

自定義事件

要在setup中,使用事件,需要借助defineEmits,這也是是一個(gè)僅能在sfc-setup語(yǔ)法中使用的編譯器宏。

<script setup lang="ts">
  // 定義事件,同時(shí)做類型注解
  // 非TS寫(xiě)法:const emits = defineEmits(['create'])
  const emits = defineEmits<{
    (e: 'create', value: string): void
  }>()
  
  // 觸發(fā)事件
  const addTodo = () => {
    emits('create', 'hi')
 }
</script>

補(bǔ)充一個(gè)用Vue3 + ts 重構(gòu)的官方TodoMVC例子:codesandbox.io/s/vibrant-w…

總結(jié)

到此這篇關(guān)于vue3.0 sfc中setup變化的文章就介紹到這了,更多相關(guān)vue3.0 sfc中setup變化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue使用threeJs導(dǎo)入obj模型并實(shí)現(xiàn)添加標(biāo)注

    vue使用threeJs導(dǎo)入obj模型并實(shí)現(xiàn)添加標(biāo)注

    這篇文章主要介紹了vue使用threeJs導(dǎo)入obj模型并實(shí)現(xiàn)添加標(biāo)注方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • vue中this.$router.push()路由傳值和獲取的兩種常見(jiàn)方法匯總

    vue中this.$router.push()路由傳值和獲取的兩種常見(jiàn)方法匯總

    這篇文章主要介紹了vue中this.$router.push()路由傳值和獲取的兩種常見(jiàn)方法,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • Vue3.0的優(yōu)化總結(jié)

    Vue3.0的優(yōu)化總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于Vue3.0的優(yōu)化總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2020-10-10
  • vueJs函數(shù)toRaw?markRaw使用對(duì)比詳解

    vueJs函數(shù)toRaw?markRaw使用對(duì)比詳解

    這篇文章主要為大家介紹了vueJs函數(shù)toRaw?markRaw使用對(duì)比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Vue3+Vite實(shí)現(xiàn)動(dòng)態(tài)路由的詳細(xì)實(shí)例代碼

    Vue3+Vite實(shí)現(xiàn)動(dòng)態(tài)路由的詳細(xì)實(shí)例代碼

    我們?cè)陂_(kāi)發(fā)大型系統(tǒng)的時(shí)候一般都需要?jiǎng)討B(tài)添加路由,下面這篇文章主要給大家介紹了關(guān)于Vue3+Vite實(shí)現(xiàn)動(dòng)態(tài)路由的相關(guān)資料,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • 在Vue中實(shí)現(xiàn)圖表數(shù)據(jù)的動(dòng)態(tài)展示的示例代碼

    在Vue中實(shí)現(xiàn)圖表數(shù)據(jù)的動(dòng)態(tài)展示的示例代碼

    隨著數(shù)據(jù)可視化技術(shù)的發(fā)展,圖表在前端開(kāi)發(fā)中扮演著越來(lái)越重要的角色,Vue.js 作為一個(gè)流行的前端框架,以其靈活性和易用性,成為了實(shí)現(xiàn)圖表動(dòng)態(tài)展示的理想選擇,在這篇博客中,我們將學(xué)習(xí)如何在 Vue 3 中實(shí)現(xiàn)動(dòng)態(tài)展示圖表數(shù)據(jù),需要的朋友可以參考下
    2024-11-11
  • Vue使用mounted和created時(shí),this無(wú)法指向data中的數(shù)據(jù)問(wèn)題

    Vue使用mounted和created時(shí),this無(wú)法指向data中的數(shù)據(jù)問(wèn)題

    這篇文章主要介紹了Vue使用mounted和created時(shí),this無(wú)法指向data中的數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • vue-cli 3 全局過(guò)濾器的實(shí)例代碼詳解

    vue-cli 3 全局過(guò)濾器的實(shí)例代碼詳解

    這篇文章主要介紹了vue-cli 3 全局過(guò)濾器的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • 一次搞清Vue3中組件通訊的全部方式

    一次搞清Vue3中組件通訊的全部方式

    毫無(wú)疑問(wèn),組件通訊是Vue中非常重要的技術(shù)之一,它的出現(xiàn)能夠使我們非常方便的在不同組件之間進(jìn)行數(shù)據(jù)的傳遞,以達(dá)到數(shù)據(jù)交互的效果,所以,學(xué)習(xí)組件通訊技術(shù)是非常有必要的,本文將一次解決Vue3中組件通訊的全部方式,總有你想要的那一種,需要的朋友可以參考下
    2024-09-09
  • vue + element動(dòng)態(tài)多表頭與動(dòng)態(tài)插槽

    vue + element動(dòng)態(tài)多表頭與動(dòng)態(tài)插槽

    這篇文章主要介紹了vue + element動(dòng)態(tài)多表頭與動(dòng)態(tài)插槽,下面文章圍繞vue + element動(dòng)態(tài)多表頭與動(dòng)態(tài)插槽的相關(guān)資料展開(kāi)文章的內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)大家有所幫助
    2021-12-12

最新評(píng)論

阳城县| 桂平市| 铁力市| 宁都县| 白沙| 罗山县| 合阳县| 婺源县| 怀安县| 尤溪县| 宁阳县| 开封市| 澄城县| 延川县| 嘉黎县| 辽阳县| 铜陵市| 卢氏县| 营口市| 涞源县| 广西| 汉中市| 大荔县| 民勤县| 奎屯市| 定襄县| 延边| 青神县| 沁阳市| 瓦房店市| 鄱阳县| 平潭县| 邢台县| 靖西县| 芜湖县| 正安县| 灵台县| 太湖县| 易门县| 长泰县| 四川省|