欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

TypeScript在vue中的使用解讀

 更新時(shí)間:2023年02月08日 09:47:04   作者:霞霞要乖  
這篇文章主要介紹了TypeScript在vue中的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

主要介紹 TypeScript 在 vue 中的使用,還有一些j注釋起來(lái)的 js 代碼做對(duì)照

參考鏈接:合成 API 的TypeScript

vue3中配合使用TS,還需要額外安裝一個(gè)vscode插件:Typescript Vue Plugin

Typescript Vue Plugin

1. 父?jìng)髯?defineProps

父組件中

<script setup lang="ts">
  import { ref } from 'vue';
  import MyComVue from './components/MyCom.vue';

  let money = ref(100)
</script>

<template>
  <div style="padding: 20px; margin: 20px; border: 1px solid red">
    <h1>父組件</h1>
    <p>傳給子組件:{{money}}</p>
    <MyComVue
      :money="money"
      car="特斯拉"
    />
    <MyComVue
      :money="money"
    />
  </div>
</template>

子組件中

<script setup lang="ts">
  // 1. js中
  // const props = defineProps({
  //   money:{
  //     type: Number,
  //     require: true
  //   },
  //   car: {
  //     type: String,
  //     required: true
  //   } 
  // })
  // 2. ts中
  // props可以通過(guò)解構(gòu)來(lái)指定默認(rèn)值,將指定默認(rèn)值的變量定義為可選參數(shù)
  const {money, car='GTR'} = defineProps<{
    money: number
    car?:string
  }>()
</script>

<template>
  <div style="padding: 20px; margin: 20px; border: 1px solid red">
    <h1>子組件</h1>
    <p>從父組件接收 {{money}} {{car}}</p>
  </div>
</template>

注:提供的默認(rèn)值需要在模板中渲染,需要額外添加配置

// vite.config.js文件中

export default defineConfig({
  plugins: [vue({
    reactivityTransform: true
  })]
})

父?jìng)髯? src=

2. 子傳父 defineEmits

父組件中

<script setup lang="ts">
  import { ref } from 'vue';
  import MyComVue from './components/MyCom.vue';

  let money = ref(100)
  const event1 = (val: number) => {
    console.log('event1',val);
    money.value = val
  }
  const changeCar = (val: string) => {
    console.log('changeCar',val);
  }
</script>

<template>
  <div style="padding: 20px; margin: 20px; border: 1px solid red">
    <h1>父組件</h1>
    <p>傳給子組件:{{money}}</p>
    <MyComVue
      :money="money"
      car="特斯拉"
      @change-car="changeCar"
    />
    <MyComVue
      :money="money"
      @event1="event1"
    />
  </div>
</template>

子組件中

<script setup lang="ts">
  // 使用ts的泛型指令props類型
  const {money, car='GTR'} = defineProps<{
    money: number
    car?:string
  }>()

  // js中-- const myEnit = defineEmits(['event1'])
  // ts中
  const myEmit = defineEmits<{
    (e:'event1', money:number):void
    (e:'changeCar', val:string):void
  }>()
  const hClick = () => {
    myEmit('event1', 200)
    myEmit('changeCar', 'BWM')
  }
</script>

<template>
  <div style="padding: 20px; margin: 20px; border: 1px solid red">
    <h1>子組件</h1>
    <p>從父組件接收 {{money}} {{car}}</p>
    <button @click="hClick">emit</button>
  </div>
</template>

computedr

3. ref和computed

<script setup lang="ts">
  import {computed, ref} from 'vue'

  // 1. ref<泛型>()
  // 簡(jiǎn)單類型可以省略,復(fù)雜類型推薦使用
  // const todos = ref([{id:1, content: 'sleep', isDone: true}])
  // ref<{id:Number,content: String,sDone: Boolean}[]>([])
  const todos = ref<{
    id:Number
    content: String
    isDone: Boolean
  }[]>([])

  setTimeout(()=>{
    todos.value = [
      {id:1, content: 'sleep', isDone: true},
      {id:2, content: 'work', isDone: false}
    ]
  },1000)

  // 2. 計(jì)算屬性: 已完成數(shù)量
  // 通過(guò)泛型可以指定computed計(jì)算屬性的類型,通??梢允÷?
  const leftCount = computed(() => {
    return todos.value.filter(item => item.isDone).length
  })
</script>

<template>
  <div>
    <ul>
      <li v-for="item in todos">{{item.content}} {{item.isDone}}</li>
    </ul>
    已完成: {{leftCount}}
  </div>
</template>

computed

4. 事件處理 ($event)

$event在vue中,他是一個(gè)特殊的變量名

  • 1. 寫在回調(diào)函數(shù)中
  • 2. 固定名字
  • 3. 表示當(dāng)前的事件對(duì)象
const move = (e: MouseEvent) => {
  mouse.value.x = e.pageX
  mouse.value.y = e.pageY
}

<-- 鼠標(biāo)懸停在 $event 上會(huì)提示類型為 MouseEvent -->
<h1 @mousemove="move($event)">根組件</h1>

5. Template Ref

<template>
  <div>
    <h1 ref="refH1">ref</h1>
    <!-- 點(diǎn)擊按鈕在控制臺(tái)打印 H1 的中的value值 -->
    <button @click="hClick">click</button>
  </div>
</template>

<script setup lang="ts">
  import { ref } from 'vue';

  const refH1 = ref<null | HTMLHeadElement>(null)
  const hClick = () => {
    console.log(refH1.value?.innerHTML);
  }
</script>

6. 可選鏈操作符

可選鏈操作符( ?. )允許讀取位于連接對(duì)象鏈深處的屬性的值,而不必明確驗(yàn)證鏈中的每個(gè)引用是否有效。

let nestedProp = obj.first?.second;
console.log(res.data?.data)
obj.fn?.()

if (obj.fn) {
    obj.fn()
}
obj.fn && obj.fn()

// 等價(jià)于
let temp = obj.first;
let nestedProp = ((temp === null || temp === undefined) ? undefined : temp.second);

7.非空斷言

如果我們明確的知道對(duì)象的屬性一定不會(huì)為空,那么可以使用非空斷言 !

// 告訴typescript, 明確的指定obj不可能為空
let nestedProp = obj!.second;

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue-cli4.x創(chuàng)建企業(yè)級(jí)項(xiàng)目的方法步驟

    vue-cli4.x創(chuàng)建企業(yè)級(jí)項(xiàng)目的方法步驟

    這篇文章主要介紹了vue-cli4.x創(chuàng)建企業(yè)級(jí)項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • vue實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)綁定

    vue實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)綁定

    本篇文章主要介紹了vue實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)綁定,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • 在vue中利用v-html按分號(hào)將文本換行的例子

    在vue中利用v-html按分號(hào)將文本換行的例子

    今天小編就為大家分享一篇在vue中利用v-html按分號(hào)將文本換行的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • Vue函數(shù)式組件-你值得擁有

    Vue函數(shù)式組件-你值得擁有

    這篇文章主要介紹了Vue函數(shù)式組件及vue函數(shù)式組件的優(yōu)缺點(diǎn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • 詳解Vue-Cli 異步加載數(shù)據(jù)的一些注意點(diǎn)

    詳解Vue-Cli 異步加載數(shù)據(jù)的一些注意點(diǎn)

    本篇文章主要介紹了詳解Vue-Cli 異步加載數(shù)據(jù)的一些注意點(diǎn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • Vue.js項(xiàng)目前端多語(yǔ)言方案的思路與實(shí)踐

    Vue.js項(xiàng)目前端多語(yǔ)言方案的思路與實(shí)踐

    前端的國(guó)際化是一個(gè)比較常見(jiàn)的需求,但網(wǎng)上關(guān)于這一方面的直接可用的方案卻不多,這篇文章主要給大家介紹了關(guān)于Vue.js項(xiàng)目前端多語(yǔ)言方案的思路與實(shí)踐,需要的朋友可以參考下
    2021-07-07
  • vue3+ElementPlus使用lang="ts"報(bào)Unexpected?token錯(cuò)誤的解決辦法

    vue3+ElementPlus使用lang="ts"報(bào)Unexpected?token錯(cuò)誤的解決

    最近開發(fā)中遇到了些問(wèn)題,跟大家分享下,這篇文章主要給大家介紹了關(guān)于vue3+ElementPlus使用lang="ts"報(bào)Unexpected?token錯(cuò)誤的解決辦法,需要的朋友可以參考下
    2023-01-01
  • Vue.js實(shí)現(xiàn)微信過(guò)渡動(dòng)畫左右切換效果

    Vue.js實(shí)現(xiàn)微信過(guò)渡動(dòng)畫左右切換效果

    這篇文章主要給大家介紹了利用Vue.js仿微信過(guò)渡動(dòng)畫左右切換效果的相關(guān)資料,需要用到的技術(shù)棧是Vue+Vuex。文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-06-06
  • vue獲取DOM節(jié)點(diǎn)的常用方法

    vue獲取DOM節(jié)點(diǎn)的常用方法

    這篇文章主要給大家介紹了vue獲取DOM節(jié)點(diǎn)的常用方法,使用ref屬性,使用$el屬性,使用querySelector和querySelectorAll,使用$refs和querySelector,這幾種方法,需要的朋友可以參考下
    2023-10-10
  • 簡(jiǎn)單理解vue中track-by屬性

    簡(jiǎn)單理解vue中track-by屬性

    這篇文章主要幫助大家簡(jiǎn)單的理解vue中track-by屬性,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10

最新評(píng)論