Vue3中使用匿名函數(shù)的方法實(shí)現(xiàn)
概述
綁定匿名方法和綁定普通方法主要是寫法上的區(qū)別,其他的區(qū)別并不是很大。
就拿上一個(gè)案例來(lái)說(shuō),我們使用匿名函數(shù)可以很簡(jiǎn)單的重寫。
基本用法
我們創(chuàng)建src/components/Demo24.vue,在這個(gè)組件中,我們要:
- 1:定義count,表示用戶點(diǎn)擊的次數(shù)
- 2:用戶每點(diǎn)擊一次按鈕,count的次數(shù)增加
- 3:如果用戶點(diǎn)擊超過100次,我們提示“看樣子你真的很喜歡這篇文章的嘞”
- 4:如果用戶點(diǎn)擊超過1000次,我們提示“作者有你這樣的粉絲真實(shí)太好了嘞”
代碼如下:
<script setup>
import {ref} from "vue";
const count = ref(0)
const onClick = () => {
count.value++
if (count.value === 100) {
alert("看樣子你真的很喜歡這篇文章的嘞")
} else if (count.value === 1000) {
alert("作者有你這樣的粉絲真實(shí)太好了嘞")
}
}
</script>
<template>
<div>
<h3>當(dāng)前點(diǎn)擊次數(shù):{{ count }}</h3>
<button @click="onClick">點(diǎn)擊</button>
</div>
</template>
<style>
span {
margin: 15px;
}
</style>
接著,我們修改src/App.vue,引入Demo24.vue并進(jìn)行渲染:
<script setup> import Demo from "./components/Demo24.vue" </script> <template> <h1>歡迎跟著Python私教一起學(xué)習(xí)Vue3入門課程</h1> <hr> <Demo/> </template>
然后,我們?yōu)g覽器訪問:http://localhost:5173/

代碼分析
修改之前的方法:
function onClick(){
count.value++
if (count.value===100){
alert("看樣子你真的很喜歡這篇文章的嘞")
}else if (count.value===1000){
alert("作者有你這樣的粉絲真實(shí)太好了嘞")
}
}
修改之后的方法:
function onClick(){
count.value++
if (count.value===100){
alert("看樣子你真的很喜歡這篇文章的嘞")
}else if (count.value===1000){
alert("作者有你這樣的粉絲真實(shí)太好了嘞")
}
}
完整代碼
package.json
{
"name": "hello",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"dependencies": {
"vue": "^3.3.8"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.5.0",
"vite": "^5.0.0"
}
}
vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
})
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" rel="external nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Vue</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
src/main.js
import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')
src/App.vue
<script setup> import Demo from "./components/Demo24.vue" </script> <template> <h1>歡迎跟著Python私教一起學(xué)習(xí)Vue3入門課程</h1> <hr> <Demo/> </template>
src/components/Demo24.vue
<script setup>
import {ref} from "vue";
const count = ref(0)
function onClick(){
count.value++
if (count.value===100){
alert("看樣子你真的很喜歡這篇文章的嘞")
}else if (count.value===1000){
alert("作者有你這樣的粉絲真實(shí)太好了嘞")
}
}
</script>
<template>
<div>
<h3>當(dāng)前點(diǎn)擊次數(shù):{{count}}</h3>
<button @click="onClick">點(diǎn)擊</button>
</div>
</template>
<style>
span{
margin: 15px;
}
</style>
啟動(dòng)方式
yarn yarn dev
瀏覽器訪問:http://localhost:5173/
到此這篇關(guān)于Vue3中使用匿名函數(shù)的方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue3 匿名函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Element-ui設(shè)置el-table表頭全選框隱藏或禁用
這篇文章主要給大家介紹了關(guān)于Element-ui設(shè)置el-table表頭全選框隱藏或禁用的相關(guān)資料,文中手把手教你實(shí)現(xiàn)el-table實(shí)現(xiàn)跨表格禁用選項(xiàng),需要的朋友可以參考下2023-07-07
Vue2.0 實(shí)現(xiàn)移動(dòng)端圖片上傳功能
本文主要介紹VUE2.0圖片上傳功能的實(shí)現(xiàn)。原理是通過js控制和input標(biāo)簽的方式完成這一效果,無(wú)需加載其他組件。具體實(shí)例大家大家參考下本文2018-05-05
vue3+vite+vant4手機(jī)端項(xiàng)目實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于vue3+vite+vant4手機(jī)端項(xiàng)目實(shí)戰(zhàn)的相關(guān)資料,Vue3是一種前端開發(fā)框架,它的目標(biāo)是提供更好的性能和開發(fā)體驗(yàn),需要的朋友可以參考下2023-08-08
解讀Vue3中keep-alive和動(dòng)態(tài)組件的實(shí)現(xiàn)邏輯
這篇文章主要介紹了Vue3中keep-alive和動(dòng)態(tài)組件的實(shí)現(xiàn)邏輯,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
Vue使用extend動(dòng)態(tài)創(chuàng)建組件的實(shí)現(xiàn)
本文主要介紹了Vue使用extend動(dòng)態(tài)創(chuàng)建組件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Vue.js組件使用props傳遞數(shù)據(jù)的方法
這篇文章主要為大家詳細(xì)介紹了Vue.js組件使用props傳遞數(shù)據(jù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
vscode 開發(fā)Vue項(xiàng)目的方法步驟
這篇文章主要介紹了vscode 開發(fā)Vue項(xiàng)目的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-11-11

