本文展示了将香草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中文网其他相关文章!

在本周的综述中:datepickers正在让键盘用户头痛,一个新的Web组件编译器,有助于与Fouc进行战斗,我们终于获得了造型列表项目标记,以及在您的网站上获得网络攻击的四个步骤。

在本周的Web平台新闻世界中,Google搜索控制台可以更轻松地查看爬行的标记,我们了解到自定义属性


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

WebStorm Mac版
好用的JavaScript开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。