本文展示了將香草JavaScript倒數計時器重構為可重複使用的VUE組件。原始計時器在上一篇文章中詳細介紹,缺乏可重複性和有效的UI同步。這種轉換解決了這些缺點。
為什麼要使用vue?主要是有兩個原因:
-
同步UI和計時器狀態:原始javaScript代碼在
timerInterval
函數中託管狀態,直接操縱DOM元素。 Vue的模板語法聲明將DOM綁定到組件的數據,簡化了UI更新。 - 可重用性:原始計時器依賴於元素ID,從而限制了其可重複使用性。 VUE組件封裝了其邏輯,從而在單個頁面上啟用了多個獨立的計時器實例。
這是VUE實施:
模板和样式
VUE使用基於HTML的模板系統。我們將創建一個帶有以下結構的BaseTimer.vue
文件:
<code><template> </template> <script> // ... </script> <style scoped> /* ... */ </style></code>
這<template></template>
部分包含計時器的標記(主要是上一篇文章中的SVG),
<template> <div class="base-timer"> <svg viewbox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"> <g> <circle cx="50" cy="50" r="45"></circle> <path :class="remainingPathColor" :stroke-dasharray="circleDasharray" d=" M 50, 50 m -45, 0 a 45,45 0 1,0 90,0 a 45,45 0 1,0 -90,0 "></path> </g> </svg> {{ formattedTimeLeft }} </div> </template> <style scoped> .base-timer { position: relative; width: 100px; height: 100px; } </style>計時器的關鍵方面是通過數據綁定來控制的: <code>stroke-dasharray</code> , <code>remainingPathColor</code>和<code>formatTime(timeLeft)</code> 。<h3 id="常數和變量">常數和變量</h3><p>這<script> section defines constants and variables. Constants, such as <code>FULL_DASH_ARRAY, <code>WARNING_THRESHOLD, <code>ALERT_THRESHOLD, and <code>COLOR_CODES, are defined directly.</script></p> <p>Variables are categorized: those directly re-assigned in methods (<code>timerInterval</code>, <code>timePassed</code>) and those dependent on other variables (<code>timeLeft</code>, <code>remainingPathColor</code>).</p> <h4 id="Reactive-Variables">Reactive Variables</h4> <p>Variables directly modified in methods are declared within the <code>data()</code> method to leverage Vue's reactivity system:</p> <pre class="brush:php;toolbar:false">data() { return { timePassed: 0, timerInterval: null }; },
Computed Properties
Variables dependent on other variables are implemented as computed
properties:
computed: { timeLeft() { return TIME_LIMIT - this.timePassed; }, circleDasharray() { return `${(this.timeFraction * FULL_DASH_ARRAY).toFixed(0)} 283`; }, formattedTimeLeft() { // ... (time formatting logic) ... }, timeFraction() { // ... (time fraction calculation) ... }, remainingPathColor() { // ... (color calculation based on timeLeft) ... } },
Computed properties are pure functions, cached for efficiency.
Using Data and Computed Properties in the Template
The template utilizes text interpolation ({{ ... }}
) and v-bind
(or its shorthand :
) directives to dynamically bind data and computed properties to the DOM.
Methods and Lifecycle Hooks
The startTimer
method, simplified due to the use of computed properties, is called within the mounted()
lifecycle hook:
methods: { startTimer() { this.timerInterval = setInterval(() => (this.timePassed = 1), 1000); } }, mounted() { this.startTimer(); },
Component Usage
To use the BaseTimer
component in another component (e.g., App.vue
):
- Import:
import BaseTimer from "./components/BaseTimer";
- Register:
components: { BaseTimer }
- Instantiate:
<basetimer></basetimer>
in the template.
This refactoring demonstrates the benefits of using Vue components for improved code organization, reusability, and efficient state management. The resulting component is self-contained and easily integrated into larger applications.
以上是從香草JavaScript移動到可重複使用的Vue組件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

對於Astro,我們可以在構建過程中生成大部分網站,但是有一小部分服務器端代碼可以使用Fuse.js之類的搜索功能來處理搜索功能。在此演示中,我們將使用保險絲搜索一組個人“書籤”


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版