久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      超詳細(xì)!圖文講解Vue3的組合式API!

      組合式API

      • 組合式api(Composition API)算是vue3對(duì)我們開發(fā)者來說非常有價(jià)值的一個(gè)api更新,我們先不關(guān)注具體語法,先對(duì)它有一個(gè)大的感知

      1. composition vs options

      • options API開發(fā)出來的vue應(yīng)用如左圖所示,它的特點(diǎn)是理解容易,因?yàn)楦鱾€(gè)選項(xiàng)都有固定的書寫位置,比如響應(yīng)式數(shù)據(jù)就寫到data選擇中,操作方法就寫到methods配置項(xiàng)中等,應(yīng)用大了之后,相信大家都遇到過來回上下找代碼的困境
      • composition API開發(fā)的vue應(yīng)用如右圖所示,它的特點(diǎn)是特定功能相關(guān)的所有東西都放到一起維護(hù),比如功能A相關(guān)的響應(yīng)式數(shù)據(jù),操作數(shù)據(jù)的方法等放到一起,這樣不管應(yīng)用多大,都可以快讀定位到某個(gè)功能的所有相關(guān)代碼,維護(hù)方便,設(shè)置如果功能復(fù)雜,代碼量大,我們還可以進(jìn)行邏輯拆分處理【推薦:vue視頻教程】
        超詳細(xì)!圖文講解Vue3的組合式API!
        超詳細(xì)!圖文講解Vue3的組合式API!
        特別注意:
      • 選項(xiàng)式api和組合式api倆種風(fēng)格是并存的關(guān)系 并不是非此即彼

      • 需要大量的邏輯組合的場(chǎng)景,可以使用compition API進(jìn)行增強(qiáng)

      2. 案例對(duì)比

      上面我們通過圖示簡(jiǎn)單了解了一下vue3帶來的全新的api形式,下面我們通過一個(gè)具體的小案例更加深入的體會(huì)一下倆種api下的開發(fā)模式對(duì)比,我們先暫時(shí)忽略語法細(xì)節(jié),只關(guān)注代碼編寫形式

      2.1 理解需求

      超詳細(xì)!圖文講解Vue3的組合式API!
      倆個(gè)獨(dú)立的功能:

      • 通過點(diǎn)擊按鈕來控制p的顯示和隱藏

      • 通過點(diǎn)擊按鈕控制p內(nèi)字體顏色的變化

      2.2 vue2.x option Api版本

      <template>   <div>     <!-- 功能一模板 -->     <button @click="show">顯示</button>     <button @click="hide">隱藏</button>     <div v-if="showDiv">一個(gè)被控制顯隱的div</div>   </div>   <div>     <!-- 功能二模板 -->     <button @click="changeRed">紅色</button>     <button @click="changeYellow">藍(lán)色</button>     <div :style="`color:${fontColor}`">一個(gè)被控制字體顏色的的div</div>   </div> </template>  <script> export default {   name: 'App',   data() {     return {       showDiv: true, // 功能一數(shù)據(jù)       fontColor: '' // 功能二數(shù)據(jù)     }   },   methods: {     // 功能一方法     show() {       this.showDiv = true     },     hide() {       this.showDiv = false     },     // 功能二方法     changeRed() {       this.fontColor = 'red'     },     changeYellow() {       this.fontColor = 'blue'     }   } } </script>

      2.3 vue3.0 composition api版本

      <template>   <div>     <!-- 功能一模板 -->     <button @click="show">顯示</button>     <button @click="hide">隱藏</button>     <div v-if="showDivFlag">一個(gè)被控制顯隱的div</div>   </div>   <div>     <!-- 功能二模板 -->     <button @click="changeRed">紅色</button>     <button @click="changeBlue">藍(lán)色</button>     <div :style="`color:${fontColor}`">一個(gè)被控制字體顏色的的div</div>   </div> </template>  <script> import { ref } from 'vue' export default {   name: 'App',   setup() {     // 功能一     const showDivFlag = ref(true)     function show() {       showDivFlag.value = true     }     function hide() {       showDivFlag.value = false     }     // 功能二      const fontColor = ref('')     function changeRed() {       fontColor.value = 'red'     }     function changeBlue() {       fontColor.value = 'blue'     }     return { showDivFlag, show, hide, fontColor, changeRed, changeBlue }   } } </script>

      2.4 composition api版本優(yōu)化

      在這里可能會(huì)有疑惑,那我們現(xiàn)在是把功能相關(guān)的所有數(shù)據(jù)和行為放到一起維護(hù)了,如果應(yīng)用很大功能很多的情況下,setup函數(shù)不會(huì)變得很大嗎?豈不是又會(huì)變得比較難維護(hù),接下來我們就來拆解一下龐大的setup函數(shù)

      <script>import { ref } from 'vue'function useShow() {   const showpFlag = ref(true)   function show() {     showpFlag.value = true   }   function hide() {     showpFlag.value = false   }   return { showpFlag, show, hide }}function useColor() {   const fontColor = ref('')   function changeRed() {     fontColor.value = 'red'   }   function changeBlue() {     fontColor.value = 'blue'   }   return { fontColor, changeRed, changeBlue }}export default {   name: 'App',   setup() {     // 功能一     const { showpFlag, show, hide } = useShow()     // 功能二     const { fontColor, changeRed, changeBlue } = useColor()     return { showpFlag, show, hide, fontColor, changeRed, changeBlue }   }}</script>

      以上,我們通過定義功能函數(shù),把倆個(gè)功能相關(guān)的代碼各自抽離到一個(gè)獨(dú)立的小函數(shù)中,然后通過在setUp函數(shù)中再把倆個(gè)小功能函數(shù)組合起來,這樣一來,我們既可以把setup函數(shù)變得清爽,又可以方便維護(hù)快速定位功能位置

      到此我們沒有關(guān)注api細(xì)節(jié),只是體會(huì)組合式api給到我們的好處,接下來我們就要深入到api細(xì)節(jié),看看全新的api都該如何使用 ↓

      3. setup入口函數(shù)

      1. setup 函數(shù)是一個(gè)新的組件選項(xiàng),作為組件中組合式API 的起點(diǎn)(入口)
      2. setup 中不能使用 this, this 指向 undefined
      3. setup函數(shù)只會(huì)在組件初始化的時(shí)候執(zhí)行一次
      4. setup函數(shù)在beforeCreate生命周期鉤子執(zhí)行之前執(zhí)行
      export default {   setup () {     console.log('setup執(zhí)行了')     console.log(this)   },   beforeCreate() {     console.log('beforeCreate執(zhí)行了')     console.log(this)   }}

      4. 響應(yīng)式系統(tǒng)API

      4.1 reactive 函數(shù)

      • 作用:reactive是一個(gè)函數(shù),接收一個(gè)普通的對(duì)象傳入,把對(duì)象數(shù)據(jù)轉(zhuǎn)化為響應(yīng)式對(duì)象并返回

      使用步驟

      • 從vue框架中導(dǎo)入reactive函數(shù)

      • 在setup函數(shù)中調(diào)用reactive函數(shù)并將對(duì)象數(shù)據(jù)傳入

      • 在setup函數(shù)中把reactive函數(shù)調(diào)用完畢之后的返回值以對(duì)象的形式返回出去

      代碼落地

      <template>   <div>{{ state.name }}</div>   <div>{{ state.age }}</div>   <button @click="state.name = 'pink'">改值</button> </template>  <script> import { reactive } from 'vue' export default {   setup () {     const state = reactive({       name: 'cp',       age: 18     })     return {       state     }   } } </script>

      4.2 ref 函數(shù)

      • 作用:ref是一個(gè)函數(shù),接受一個(gè)簡(jiǎn)單類型或者復(fù)雜類型的傳入并返回一個(gè)響應(yīng)式且可變的 ref 對(duì)象

      使用步驟

      • 從vue框架中導(dǎo)出ref函數(shù)

      • 在setup函數(shù)中調(diào)用ref函數(shù)并傳入數(shù)據(jù)(簡(jiǎn)單類型或者復(fù)雜類型)

      • 在setup函數(shù)中把ref函數(shù)調(diào)用完畢的返回值以對(duì)象的形式返回出去

      • 注意:在setup函數(shù)中使用ref結(jié)果,需要通過.value 訪問,模板中使用不需要加.value

      <template>   <div>{{ money }}</div>   <button @click="changeMondy">改值</button> </template>  <script> import { ref } from 'vue' export default {   setup() {     let money = ref(100)     console.log(money.value)     return {       money     }   } } </script>

      總結(jié)說明:

      • ref 函數(shù)可以接收一個(gè)簡(jiǎn)單類型的值,返回一個(gè)可改變的 ref 響應(yīng)式對(duì)象,從而彌補(bǔ)reactive函數(shù)不支持簡(jiǎn)單類型的問題

      • reactive和ref函數(shù)都可以提供響應(yīng)式數(shù)據(jù)的轉(zhuǎn)換,具體什么時(shí)候需要使用哪個(gè)API社區(qū)還沒有最佳實(shí)踐,大家暫時(shí)可以使用自己熟練的API進(jìn)行轉(zhuǎn)換

      • 推薦一種寫法:只有我們明確知道要轉(zhuǎn)換的對(duì)象內(nèi)部的字段名稱我們才使用reactive,否則就一律使用ref,從而降低在語法選擇上的心智負(fù)擔(dān)

      4.3 toRefs 函數(shù)

      • 場(chǎng)景: 經(jīng)過reactive函數(shù)處理之后返回的對(duì)象,如果給這個(gè)對(duì)象解構(gòu)或者展開,會(huì)讓數(shù)據(jù)丟失響應(yīng)式的能力,為了解決這個(gè)問題需要引入toRefs函數(shù),使用 toRefs函數(shù) 可以保證該對(duì)象展開的每個(gè)屬性都是響應(yīng)式的

      4.3.1 問題復(fù)現(xiàn)

      還是之前的案例,如果我們想在模板中省略到state,直接書寫name和age,你可能會(huì)想到,那我在return出去的時(shí)候把state中的屬性解構(gòu)出來不就好了

      修改前

      <template>   <div>{{ state.name }}</div>   <div>{{ state.age }}</div>   <button @click="state.name = 'pink'">改值</button> </template>  <script> import { reactive } from 'vue' export default {   setup() {     const state = reactive({       name: 'cp',       age: 18     })     return {       state     }   } } </script>

      解構(gòu)修改后

      <template>   <div>{{ name }}</div>   <div>{{ age }}</div>   <button @click="name = 'pink'">改值</button> </template> <script> import { reactive } from 'vue' export default {   setup() {     const state = reactive({       name: 'cp',       age: 18     })     return {       ...state     }   } } </script>
      • 點(diǎn)擊改值按鈕,發(fā)現(xiàn)視圖已經(jīng)不發(fā)生變化了,這就是我們所說的,如果解構(gòu)reactive的返回值,將破壞調(diào)用響應(yīng)式特性,就需要我們使用toRefs方法進(jìn)行處理了

      4.3.2 toRefs包裹處理

      <template>   <div>{{ name }}</div>   <div>{{ age }}</div>   <button @click="name = 'pink'">改值</button> </template>  <script> import { reactive,toRefs } from 'vue' export default {   setup() {     const state = reactive({       name: 'cp',       age: 18     })     return {       ...toRefs(state)     }   } } </script>

      4.4 computed

      • 在setup函數(shù)中使用計(jì)算屬性函數(shù)

      作用:根據(jù)現(xiàn)有響應(yīng)式數(shù)據(jù)經(jīng)過一定的計(jì)算得到全新的數(shù)據(jù)

      使用步驟

      • 從vue框架中導(dǎo)入computed 函數(shù)

      • 在setup函數(shù)中執(zhí)行computed函數(shù),并傳入一個(gè)函數(shù),在函數(shù)中定義計(jì)算公式

      • 把computed函數(shù)調(diào)用完的執(zhí)行結(jié)果放到setup的return值對(duì)象中

      <template>   {{ list }}   {{ filterList }}  <button @click="changeList">change list</button></template><script>import { computed, ref } from 'vue'export default {   setup() {     const list = ref([1, 2, 3, 4, 5])     // 輸入大于3的數(shù)字     const filterList = computed(() => {       return list.value.filter(item => item > 3)     })     // 修改list的函數(shù)     function changeList() {       list.value.push(6, 7, 8)     }     return {       list,       filterList,       changeList    }   }}</script>

      4.5 watch 偵聽器

      • 在setup函數(shù)中偵聽器的使用

      作用:基于響應(yīng)式數(shù)據(jù)的變化執(zhí)行回調(diào)邏輯,和vue2中的watch的功能完全一致

      • 普通監(jiān)聽

      • 立即執(zhí)行

      • 深度監(jiān)聽

      使用步驟

      • 從vue框架中導(dǎo)入watch函數(shù)

      • 在setup函數(shù)中執(zhí)行watch函數(shù)開啟對(duì)響應(yīng)式數(shù)據(jù)的監(jiān)聽

      • watch函數(shù)接收三個(gè)常規(guī)參數(shù)

        1. 第一個(gè)參數(shù)為函數(shù),返回你要監(jiān)聽變化的響應(yīng)式數(shù)據(jù)
        2. 第二個(gè)參數(shù)為響應(yīng)式數(shù)據(jù)變化之后要執(zhí)行的回調(diào)函數(shù)
        3. 第三個(gè)參數(shù)為一個(gè)對(duì)象,在里面配置是否開啟立刻執(zhí)行或者深度監(jiān)聽

      4.5.1 普通監(jiān)聽

      <template>   {{ age }}  <button @click="age++">change age</button></template><script>import { ref, watch } from 'vue'export default {   setup() {     const age = ref(18)     watch(() => {       // 返回你想要監(jiān)聽的響應(yīng)式屬性(ref產(chǎn)生的對(duì)象必須加.value)       return age.value    }, () => {       // 數(shù)據(jù)變化之后的回調(diào)函數(shù)       console.log('age發(fā)生了變化')     })     return {       age    }   }}</script>

      4.5.2 開啟立刻執(zhí)行

      watch的效果默認(rèn)狀態(tài)下,只有監(jiān)聽的數(shù)據(jù)發(fā)生變化才會(huì)執(zhí)行回調(diào),如果你需要在一上來的時(shí)候就立刻執(zhí)行一次,需要配置一下immediate屬性

      <template>   {{ age }}  <button @click="age++">change age</button></template><script>import { ref, watch } from 'vue'export default {   setup() {     const age = ref(18)     watch(() => {       // 返回你想要監(jiān)聽的響應(yīng)式屬性(ref產(chǎn)生的對(duì)象必須加.value)       return age.value    }, () => {       // 數(shù)據(jù)變化之后的回調(diào)函數(shù)       console.log('age發(fā)生了變化')     },{ immediate: true})     return {       age    }   }}</script>

      4.5.3 開啟深度監(jiān)聽

      當(dāng)我們監(jiān)聽的數(shù)據(jù)是一個(gè)對(duì)象的時(shí)候,默認(rèn)狀態(tài)下,對(duì)象內(nèi)部的屬性發(fā)生變化是不會(huì)引起回調(diào)函數(shù)執(zhí)行的,如果想讓對(duì)象下面所有屬性都能得到監(jiān)聽,需要開啟deep配置

      <template>   {{ name }}   {{ info.age }}  <button @click="name = 'pink'">change name</button>   <button @click="info.age++">change age</button></template><script>import { reactive, toRefs, watch } from 'vue'export default {   setup() {     const state = reactive({       name: 'cp',       info: {         age: 18       }     })     watch(() => {       return state    }, () => {       // 數(shù)據(jù)變化之后的回調(diào)函數(shù)       console.log('age發(fā)生了變化')     }, {       deep: true     })     return {       ...toRefs(state)     }   }}</script>

      4.5.4 更好的做法

      使用watch的時(shí)候,盡量詳細(xì)的表明你到底要監(jiān)聽哪個(gè)屬性,避免使用deep引起的性能問題,比如我僅僅只是想在state對(duì)象的age屬性變化的時(shí)候執(zhí)行回調(diào),可以這么寫

      <template>   {{ name }}   {{ info.age }}  <button @click="name = 'pink'">change name</button>   <button @click="info.age++">change age</button></template><script>import { reactive, toRefs, watch } from 'vue'export default {   setup() {     const state = reactive({       name: 'cp',       info: {         age: 18       }     })     watch(() => {       // 詳細(xì)的告知你要監(jiān)聽誰       return state.info.age    }, () => {       // 數(shù)據(jù)變化之后的回調(diào)函數(shù)       console.log('age發(fā)生了變化')     })     return {       ...toRefs(state)     }   }}</script>

      5. 生命周期函數(shù)

      使用步驟

      • 先從vue中導(dǎo)入以on打頭的生命周期鉤子函數(shù)

      • 在setup函數(shù)中調(diào)用生命周期函數(shù)并傳入回調(diào)函數(shù)

      • 生命周期鉤子函數(shù)可以調(diào)用多次

      <template>   <div>生命周期函數(shù)</div> </template>  <script> import { onMounted } from 'vue' export default {   setup() {     // 時(shí)機(jī)成熟 回調(diào)函數(shù)自動(dòng)執(zhí)行     onMounted(() => {       console.log('mouted生命周期執(zhí)行了')     })      onMounted(() => {       console.log('mouted生命周期函數(shù)又執(zhí)行了')     })   } } </script>
      選項(xiàng)式API 組合式API
      beforeCreate 不需要(直接寫到setup函數(shù)中)
      created 不需要(直接寫到setup函數(shù)中)
      beforeMount onBeforeMount
      mounted onMounted
      beforeUpdate onBeforeUpdate
      updated onUpdated
      beforeDestroyed onBeforeUnmount
      destroyed onUnmounted

      6. 父子通信

      在vue3的組合式API中,父?jìng)髯拥幕A(chǔ)套路完全一樣,基礎(chǔ)思想依舊為:父?jìng)髯邮峭ㄟ^prop進(jìn)行傳入,子傳父通過調(diào)用自定義事件完成

      實(shí)現(xiàn)步驟

      • setup函數(shù)提供倆個(gè)參數(shù),第一個(gè)參數(shù)為props,第二個(gè)參數(shù)為一個(gè)對(duì)象context

      • props為一個(gè)對(duì)象,內(nèi)部包含了父組件傳遞過來的所有prop數(shù)據(jù),context對(duì)象包含了attrs,slots, emit屬性,其中的emit可以觸發(fā)自定義事件的執(zhí)行從而完成子傳父

      代碼落地
      app.vue

      <template>   <son :name="name" @get-msg="getMsg"></son></template><script>import { ref } from 'vue'import Son from './components/son'export default {   components: {     Son  },   setup() {     const name = ref('cp')     function getMsg(msg) {       console.log(msg)     }     return {       name,       getMsg    }   }}</script>

      components/son.vue

      <template>   <div>     {{name}}     <button @click="setMsgToSon">set</button>   </div> </template>  <script> export default {   props: {     name: {       type: String     }   },   emits: ['get-msg'], // 聲明當(dāng)前組件觸發(fā)的自定義事件   setup(props,{emit}) {     console.log(props.name)     function setMsgToSon(){       emit('get-msg','這是一條來自子組件的msg信息')     }     return {       setMsgToSon     }   } } </script>

      7. provide 和 inject

      通常我們使用props進(jìn)行父子之間的數(shù)據(jù)傳遞,但是如果組件嵌套層級(jí)較深,一層一層往下傳遞將會(huì)變的非常繁瑣,有沒有一種手段可以把這個(gè)過程簡(jiǎn)化一下呢,有的,就是我們馬上要學(xué)習(xí)的provide 和 inject,它們配合起來可以方便的完成跨層傳遞數(shù)據(jù)

      超詳細(xì)!圖文講解Vue3的組合式API!

      7.1 基礎(chǔ)使用

      • 在setup函數(shù)中使用provide和inject的基礎(chǔ)用法

      來個(gè)需求: 爺組件中有一份數(shù)據(jù) 傳遞給孫組件直接使用
      超詳細(xì)!圖文講解Vue3的組合式API!

      實(shí)現(xiàn)步驟:

      • 頂層組件在setup方法中使用provide函數(shù)提供數(shù)據(jù)

      • 任何底層組件在setup方法中使用inject函數(shù)獲取數(shù)據(jù)

      代碼落地
      爺爺組件 - app.vue

      <template>   <father></father></template><script>import Father from '@/components/Father'import { provide } from 'vue'export default {   components: {     Father  },   setup() {     let name = '柴柴老師'     // 使用provide配置項(xiàng)注入數(shù)據(jù) key - value     provide('name', name)   }}</script>

      孫組件 - components/Son.vue

      <template>   我是子組件   {{ name }}</template><script>import { inject } from 'vue'export default {   setup() {     const name = inject('name')     return {       name    }   }}</script>

      事實(shí)上,只要是后代組件,都可以方便的獲取頂層組件提供的數(shù)據(jù)

      7.2 傳遞響應(yīng)式數(shù)據(jù)

      provide默認(rèn)情況下傳遞的數(shù)據(jù)不是響應(yīng)式的,也就是如果對(duì)provide提供的數(shù)據(jù)進(jìn)行修改,并不能響應(yīng)式的影響到底層組件使用數(shù)據(jù)的地方,如果想要傳遞響應(yīng)數(shù)據(jù)也非常簡(jiǎn)單,只需要將傳遞的數(shù)據(jù)使用ref或者reactive生成即可

      • 通過provide/inject傳遞響應(yīng)式數(shù)據(jù)
        app.vue
      <template>   <father></father>   <button @click="changeName">change name</button></template><script>import Father from '@/components/Father'import { provide, ref } from 'vue'export default {   components: {     Father  },   setup() {     // 使用ref轉(zhuǎn)換成響應(yīng)式再傳遞     let name = ref('柴柴老師')     function changeName(){       name.value = 'pink'     }     provide('name', name)     return {       changeName    }   }}</script>

      8. 模板中 ref 的使用

      在模板中使用ref,我們都很清楚,它一般有三種使用場(chǎng)景

      • ref + 普通dom標(biāo)簽 獲取真實(shí)dom對(duì)象

      • ref + 組件標(biāo)簽 獲取組件實(shí)例對(duì)象

      • ref + v-for 獲取由dom對(duì)象(實(shí)例對(duì)象)組成的數(shù)組 (不經(jīng)常使用)

      • 在setup函數(shù)中使用ref獲取真實(shí)dom獲取組件實(shí)例的方法

      實(shí)現(xiàn)步驟

      • 使用ref函數(shù)傳入null創(chuàng)建 ref對(duì)象 => const hRef = ref(null)

      • 模板中通過定義ref屬性等于1中創(chuàng)建的ref對(duì)象名稱建立關(guān)聯(lián) => <h1 ref="hRef"></h1>

      • 使用 =>hRef.value

      代碼落地
      components/RefComponent.vue

      <template>   我是一個(gè)普通的組件</template>

      app.vue

      <template>   <h1 ref="h1Ref">我是普通dom標(biāo)簽</h1>   <ref-comoonent ref="comRef"></ref-comoonent></template><script>import { onMounted, ref } from 'vue'import RefComoonent from '@/components/RefComponent'export default {   components: {     RefComoonent  },   setup() {     const h1Ref = ref(null)     const comRef = ref(null)     onMounted(() => {       console.log(h1Ref.value)       console.log(comRef.value)     })     // 必須return     return {       h1Ref,       comRef    }   }}</script>

      9. 來個(gè)案例吧 – Todos

      核心功能

      • 渲染列表數(shù)據(jù) v-for

      • 點(diǎn)擊刪除當(dāng)前列表 splice + index

      • 回車添加新項(xiàng)目 @keyup.enter=“addTodo” list.unshift

      • 選擇狀態(tài)切換 v-model

      • 多選和取消多選 計(jì)算屬性的set和get

      • 未完成任務(wù)數(shù)量統(tǒng)計(jì) computed

      <template>   <section class="todoapp">     <!-- 頭部輸入框區(qū)域 -->     <header class="header">       <h1>todos</h1>       <input         class="new-todo"         placeholder="請(qǐng)輸入要完成的任務(wù)"         autofocus         v-model="curTask"         @keyup.enter="add"       />     </header>     <section class="main">       <!-- 全選切換input -->       <input id="toggle-all" class="toggle-all" type="checkbox" v-model="isAll"/>       <label for="toggle-all">標(biāo)記所有已經(jīng)完成</label>       <ul class="todo-list">         <!-- 任務(wù)列表 -->         <li v-for="(item, index) in list" :key="item.id">           <p class="view">             <!-- 雙向綁定 flag -->             <input class="toggle" type="checkbox" v-model="item.flag" />             <label>{{ item.name }}</label>             <!-- 刪除按鈕 -->             <button class="destroy" @click="del(index)"></button>           </p>         </li>       </ul>     </section>     <footer class="footer">       <span class="todo-count"> 還未完成的任務(wù)有:<strong>{{count}}</strong>項(xiàng) </span>     </footer>   </section></template><script>import { computed, ref } from 'vue'export default {   setup() {     const list = ref([       { id: 1, name: '吃飯', flag: false },       { id: 2, name: '睡覺', flag: false },       { id: 3, name: '打豆豆', flag: true }     ])      // 刪除函數(shù)     function del(index) {       // index 要?jiǎng)h除項(xiàng)的下標(biāo)值       // splice       list.value.splice(index, 1)     }      const curTask = ref('')     function add() {       // 添加邏輯       list.value.unshift({         id: new Date(),         name: curTask.value,         flag: false       })       curTask.value = ''     }      // 全選取消全選     // {name:"cp"}  console.log(info.name)  info.name = 'pink'     const isAll = computed({       // 獲取isAll數(shù)據(jù)的時(shí)候會(huì)執(zhí)行g(shù)et函數(shù)       get() {         // 當(dāng)list列表中所有項(xiàng)的flag屬性都為true 就為true         // every         return list.value.every(item => item.flag === true)       },       set(val) {         // 拿到isAll最新值 遍歷一下list 把里面的flag屬性設(shè)置為最新值         list.value.forEach(item => {           item.flag = val        })       }     })      // 計(jì)算未完成的任務(wù)     const count = computed(()=>{       return  list.value.filter(item=>item.flag === false).length    })         return {       list,       del,       curTask,       add,       isAll,       count    }   }}</script><style>html, body {   margin: 0;   padding: 0;}button {   margin: 0;   padding: 0;   border: 0;   background: none;   font-size: 100%;   vertical-align: baseline;   font-family: inherit;   font-weight: inherit;   color: inherit;   -webkit-appearance: none;   appearance: none;   -webkit-font-smoothing: antialiased;   -moz-osx-font-smoothing: grayscale;}body {   font: 14px "Helvetica Neue", Helvetica, Arial, sans-serif;   line-height: 1.4em;   background: #f5f5f5;   color: #111111;   min-width: 230px;   max-width: 550px;   margin: 0 auto;   -webkit-font-smoothing: antialiased;   -moz-osx-font-smoothing: grayscale;   font-weight: 300;}:focus {   outline: 0;}.hidden {   display: none;}.todoapp {   background: #fff;   margin: 130px 0 40px 0;   position: relative;   box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);}.todoapp input::-webkit-input-placeholder {   font-style: italic;   font-weight: 300;   color: rgba(0, 0, 0, 0.4);}.todoapp input::-moz-placeholder {   font-style: italic;   font-weight: 300;   color: rgba(0, 0, 0, 0.4);}.todoapp input::input-placeholder {   font-style: italic;   font-weight: 300;   color: rgba(0, 0, 0, 0.4);}.todoapp h1 {   position: absolute;   top: -140px;   width: 100%;   font-size: 80px;   font-weight: 200;   text-align: center;   color: #b83f45;   -webkit-text-rendering: optimizeLegibility;   -moz-text-rendering: optimizeLegibility;   text-rendering: optimizeLegibility;}.new-todo, .edit {   position: relative;   margin: 0;   width: 100%;   font-size: 24px;   font-family: inherit;   font-weight: inherit;   line-height: 1.4em;   color: inherit;   padding: 6px;   border: 1px solid #999;   box-shadow: inset 0 -1px 5px 0 rgba(0, 0, 0, 0.2);   box-sizing: border-box;   -webkit-font-smoothing: antialiased;   -moz-osx-font-smoothing: grayscale;}.new-todo {   padding: 16px 16px 16px 60px;   border: none;   background: rgba(0, 0, 0, 0.003);   box-shadow: inset 0 -2px 1px rgba(0, 0, 0, 0.03);}.main {   position: relative;   z-index: 2;   border-top: 1px solid #e6e6e6;}.toggle-all {   width: 1px;   height: 1px;   border: none; /* Mobile Safari */   opacity: 0;   position: absolute;   right: 100%;   bottom: 100%;}.toggle-all + label {   width: 60px;   height: 34px;   font-size: 0;   position: absolute;   top: -52px;   left: -13px;   -webkit-transform: rotate(90deg);   transform: rotate(90deg);}.toggle-all + label:before {   content: "?";   font-size: 22px;   color: #e6e6e6;   padding: 10px 27px 10px 27px;}.toggle-all:checked + label:before {   color: #737373;}.todo-list {   margin: 0;   padding: 0;   list-style: none;}.todo-list li {   position: relative;   font-size: 24px;   border-bottom: 1px solid #ededed;}.todo-list li:last-child {   border-bottom: none;}.todo-list li.editing {   border-bottom: none;   padding: 0;}.todo-list li.editing .edit {   display: block;   width: calc(100% - 43px);   padding: 12px 16px;   margin: 0 0 0 43px;}.todo-list li.editing .view {   display: none;}.todo-list li .toggle {   text-align: center;   width: 40px;   /* auto, since non-WebKit browsers doesn't support input styling */   height: auto;   position: absolute;   top: 0;   bottom: 0;   margin: auto 0;   border: none; /* Mobile Safari */   -webkit-appearance: none;   appearance: none;}.todo-list li .toggle {   opacity: 0;}.todo-list li .toggle + label {   background-image: url("data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23ededed%22%20stroke-width%3D%223%22/%3E%3C/svg%3E");   background-repeat: no-repeat;   background-position: center left;}.todo-list li .toggle:checked + label {   background-image: url("data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23bddad5%22%20stroke-width%3D%223%22/%3E%3Cpath%20fill%3D%22%235dc2af%22%20d%3D%22M72%2025L42%2071%2027%2056l-4%204%2020%2020%2034-52z%22/%3E%3C/svg%3E");}.todo-list li label {   word-break: break-all;   padding: 15px 15px 15px 60px;   display: block;   line-height: 1.2;   transition: color 0.4s;   font-weight: 400;   color: #4d4d4d;}.todo-list li.completed label {   color: #cdcdcd;   text-decoration: line-through;}.todo-list li .destroy {   display: none;   position: absolute;   top: 0;   right: 10px;   bottom: 0;   width: 40px;   height: 40px;   margin: auto 0;   font-size: 30px;   color: #cc9a9a;   margin-bottom: 11px;   transition: color 0.2s ease-out;}.todo-list li .destroy:hover {   color: #af5b5e;}.todo-list li .destroy:after {   content: "×";}.todo-list li:hover .destroy {   display: block;}.todo-list li .edit {   display: none;}.todo-list li.editing:last-child {   margin-bottom: -1px;}.footer {   padding: 10px 15px;   height: 20px;   text-align: center;   font-size: 15px;   border-top: 1px solid #e6e6e6;}.footer:before {   content: "";   position: absolute;   right: 0;   bottom: 0;   left: 0;   height: 50px;   overflow: hidden;   box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2), 0 8px 0 -3px #f6f6f6,     0 9px 1px -3px rgba(0, 0, 0, 0.2), 0 16px 0 -6px #f6f6f6,     0 17px 2px -6px rgba(0, 0, 0, 0.2);}.todo-count {   float: left;   text-align: left;}.todo-count strong {   font-weight: 300;}.filters {   margin: 0;   padding: 0;   list-style: none;   position: absolute;   right: 0;   left: 0;}.filters li {   display: inline;}.filters li a {   color: inherit;   margin: 3px;   padding: 3px 7px;   text-decoration: none;   border: 1px solid transparent;   border-radius: 3px;}.filters li a:hover {   border-color: rgba(175, 47, 47, 0.1);}.filters li a.selected {   border-color: rgba(175, 47, 47, 0.2);}.clear-completed, html .clear-completed:active {   float: right;   position: relative;   line-height: 20px;   text-decoration: none;   cursor: pointer;}.clear-completed:hover {   text-decoration: underline;}.info {   margin: 65px auto 0;   color: #4d4d4d;   font-size: 11px;   text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);   text-align: center;}.info p {   line-height: 1;}.info a {   color: inherit;   text-decoration: none;   font-weight: 400;}.info a:hover {   text-decoration: underline;}/* 	Hack to remove background from Mobile Safari. 	Can't use it globally since it destroys checkboxes in Firefox */@media screen and (-webkit-min-device-pixel-ratio: 0) {   .toggle-all,   .todo-list li .toggle {     background: none;   }    .todo-list li .toggle {     height: 40px;   }}@media (max-width: 430px) {   .footer {     height: 50px;   }    .filters {     bottom: 10px;   }}</style>

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)