搜索
首页web前端css教程从香草JavaScript移动到可重复使用的Vue组件

从香草JavaScript移动到可重复使用的Vue组件

本文展示了将香草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):

  1. Import: import BaseTimer from "./components/BaseTimer";
  2. Register: components: { BaseTimer }
  3. 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中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
有点提醒您伪元素是孩子,有点。有点提醒您伪元素是孩子,有点。Apr 19, 2025 am 11:39 AM

这里有一个带子元素的容器:

菜单具有'动态命中区域”菜单具有'动态命中区域”Apr 19, 2025 am 11:37 AM

飞翔的菜单!您需要在第二个菜单中实现悬停事件以显示更多菜单项的菜单,即您在棘手的领域中。一方面,他们应该

通过Webvtt改善视频可访问性通过Webvtt改善视频可访问性Apr 19, 2025 am 11:27 AM

“网络的力量在于其普遍性。每个人的访问无论残疾是一个重要方面。” - 蒂姆·伯纳斯 - 李

每周平台新闻:CSS :: Marker伪元素,预先渲染的Web组件,向您的网站添加Webmention每周平台新闻:CSS :: Marker伪元素,预先渲染的Web组件,向您的网站添加WebmentionApr 19, 2025 am 11:25 AM

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

使宽度和灵活的物品一起玩得很好使宽度和灵活的物品一起玩得很好Apr 19, 2025 am 11:23 AM

简短的答案:您可能要寻找的是弹性折射和弹性基础。

位置粘性和桌子标头位置粘性和桌子标头Apr 19, 2025 am 11:21 AM

您可以位置:粘性;一个

每周平台新闻:HTML在搜索控制台,全局脚本范围中的HTML检查,Babel Envs添加默认查询查询每周平台新闻:HTML在搜索控制台,全局脚本范围中的HTML检查,Babel Envs添加默认查询查询Apr 19, 2025 am 11:18 AM

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

Indieweb和网络企业Indieweb和网络企业Apr 19, 2025 am 11:16 AM

Indieweb是一回事!他们将举行会议和一切。纽约客甚至在写这件事:

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

SecLists

SecLists

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

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

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

安全考试浏览器

安全考试浏览器

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

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

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