2022-11-29 Vue Vue3語法糖-ref,reactive,onMounted Vue3.2 版本開始使用語法糖!script 標籤上寫上 setup!1$ <script setup></script> import { ref,reactive,onMounted } from ‘vue’;不需寫 return,直接宣告資料123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657<template> <div id="app"> <h1 v-show="data.sayShow">{{ message }}</h1> <button @click="open">Say hello.</button> </div></template><script setup>import { ref,reactive } from 'vue'; const message = ref('Hi I am Lara') const data = reactive({ sayShow: false,}) const open = () => { console.log('打開') data.sayShow = true}//TypeScript 型別設定interface listType { dt?: number | null, message?: number | null, cnt?: number | null, list:any[]}interface weatherType { cod?: number|null, message?: number | null, cnt?: number | null, list: listType[]}const weathers = ref<any>();//${ import.meta.env.VITE_API_URL }//${ import.meta.env.VITE_API_KEY }const getData = async () => { const api = `${import.meta.env.VITE_API_URL}forecast?q=Taichung,tw&APPID=${import.meta.env.VITE_API_KEY }&lang=zh_tw&units=metric` try { const data = await fetch(api); if (!data.ok) { throw Error('fetch data 失敗'); } if (data.status === 200) { weathers.value = await data.json(); console.log(await data.json()) } } catch (error) { // throw Error(error?:any) }}onMounted(() => { getData();});</script> 附上CodePen連結點擊 Newer Vue3語法糖-路useRoute和useRouter Older Vite Vue 目錄