国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目次
テンプレートとスタイル
Computed Properties
Using Data and Computed Properties in the Template
Methods and Lifecycle Hooks
Component Usage
ホームページ ウェブフロントエンド CSSチュートリアル Vanilla JavaScriptから再利用可能なVueコンポーネントに移動(dòng)します

Vanilla JavaScriptから再利用可能なVueコンポーネントに移動(dòng)します

Apr 11, 2025 am 09:40 AM

Vanilla JavaScriptから再利用可能なVueコンポーネントに移動(dòng)します

この記事では、バニラJavaScriptカウントダウンタイマーを再利用可能なVUEコンポーネントにリファクタリングすることを示しています。以前の記事で詳述されている元のタイマーは、再利用性と効率的なUI同期を欠いていました。この変換は、これらの欠點(diǎn)に対処します。

なぜVueを使用するのですか?主に2つの理由で:

  • 同期されたUIとタイマー狀態(tài): timerInterval関數(shù)內(nèi)で、DOM要素を直接操作する元のJavaScriptコードが狀態(tài)を管理しました。 Vueのテンプレート構(gòu)文は、DOMをコンポーネントのデータに宣言して結(jié)合し、UIの更新を簡(jiǎn)素化します。
  • 再利用性:元のタイマーは要素IDに依存しており、再利用性を制限しています。 VUEコンポーネントはロジックをカプセル化し、単一ページで複數(shù)の獨(dú)立したタイマーインスタンスを有効にします。

これがVUEの実裝です:

テンプレートとスタイル

VueはHTMLベースのテンプレートシステムを使用します。次の構(gòu)造を持つ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>タイマーの主要な側(cè)面は、 <code>stroke-dasharray</code> 、 <code>remainingPathColor</code> 、 <code>formatTime(timeLeft)</code>データバインディングを通じて制御されます。<h3>定數(shù)と変數(shù)</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>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.

以上がVanilla JavaScriptから再利用可能なVueコンポーネントに移動(dòng)しますの詳細(xì)內(nèi)容です。詳細(xì)については、PHP 中國(guó)語(yǔ) Web サイトの他の関連記事を參照してください。

このウェブサイトの聲明
この記事の內(nèi)容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰屬します。このサイトは、それに相當(dāng)する法的責(zé)任を負(fù)いません。盜作または侵害の疑いのあるコンテンツを見(jiàn)つけた場(chǎng)合は、admin@php.cn までご連絡(luò)ください。

ホットAIツール

Undress AI Tool

Undress AI Tool

脫衣畫(huà)像を無(wú)料で

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード寫(xiě)真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

寫(xiě)真から衣服を削除するオンライン AI ツール。

Clothoff.io

Clothoff.io

AI衣類(lèi)リムーバー

Video Face Swap

Video Face Swap

完全無(wú)料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡(jiǎn)単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無(wú)料のコードエディター

SublimeText3 中國(guó)語(yǔ)版

SublimeText3 中國(guó)語(yǔ)版

中國(guó)語(yǔ)版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強(qiáng)力な PHP 統(tǒng)合開(kāi)発環(huán)境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開(kāi)発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

「レンダリングブロッキングCSS」とは何ですか? 「レンダリングブロッキングCSS」とは何ですか? Jun 24, 2025 am 12:42 AM

ブラウザは、特にインポートされたスタイルシート、ヘッダーのインラインCSS、および最適化されていないメディアクエリスタイルを使用して、ブラウザがインラインおよび外部CSSをデフォルトで主要なリソースとして表示するため、ページレンダリングをブロックします。 1.重要なCSSを抽出し、HTMLに埋め込みます。 2。JavaScriptを介して非クリティカルなCSSの読み込みを遅らせる。 3.メディア屬性を使用して、印刷スタイルなどのロードを最適化します。 4.リクエストを減らすためにCSSを圧縮およびマージします。ツールを使用してキーCSSを抽出し、REL = "Preload"非同期負(fù)荷を組み合わせ、過(guò)度の分割と複雑なスクリプト制御を避けるためにメディア遅延荷重を合理的に使用することをお?jiǎng)幛幛筏蓼埂?/p>

外部対內(nèi)部CSS:最良のアプローチは何ですか? 外部対內(nèi)部CSS:最良のアプローチは何ですか? Jun 20, 2025 am 12:45 AM

TheBestAppRoachforCSDependsonTheProject'sSpecificNeeds.forLargerProjects、externalCssissisbetterduetoMaintainasiladability; forsmallerProjectsOrsingLe-PageApplications、internalcsSmightBemoresuitable.it

私のCSSは小文字でなければなりませんか? 私のCSSは小文字でなければなりませんか? Jun 19, 2025 am 12:29 AM

いいえ、CSSDOESNOTHAVETOBEINLOWERCASE。

CSSケース感度:重要なことを理解する CSSケース感度:重要なことを理解する Jun 20, 2025 am 12:09 AM

cssismostlycase-inssensitive、buturlsandfontfamilynamesarecase-sensitive.1)propertiesandvalueslikecolor:red; areotcase-sensitive.2)urlsmustmatchtheserver'scase、例えば、/畫(huà)像/logo.png.3)

Autoprefixerとは何ですか?それはどのように機(jī)能しますか? Autoprefixerとは何ですか?それはどのように機(jī)能しますか? Jul 02, 2025 am 01:15 AM

Autoprefixerは、ターゲットブラウザスコープに基づいてCSS屬性にベンダープレフィックスを自動(dòng)的に追加するツールです。 1.エラーで接頭辭を手動(dòng)で維持する問(wèn)題を解決します。 2. PostCSSプラグインフォーム、CSSを解析し、プレフィックスする必要がある屬性を分析し、構(gòu)成に従ってコードを生成する屬性を分析します。 3.使用手順には、プラグインのインストール、ブラウザーリストの設(shè)定、ビルドプロセスでそれらを有効にすることが含まれます。 4。メモには、接頭辭を手動(dòng)で追加しない、構(gòu)成の更新を保持すること、すべての屬性ではなくプレフィックスを維持することが含まれ、プリ??プロセッサでそれらを使用することをお?jiǎng)幛幛筏蓼埂?/p>

CSSカウンターとは何ですか? CSSカウンターとは何ですか? Jun 19, 2025 am 12:34 AM

csScountersCantAnationally-bersectionSandLists.1)usecounter-resettoinitialize、counter-incrementtoincrease、andcounter()orcounters()todisplayvalues.2)を組み合わせたjavascriptfordynamiccontenttoensureaCurateupdatesと組み合わせます。

CSS:ケースはいつ重要ですか(いつそうではありませんか)? CSS:ケースはいつ重要ですか(いつそうではありませんか)? Jun 19, 2025 am 12:27 AM

CSSでは、セレクターと屬性名はケースに敏感ですが、値、名前の色、URL、およびカスタム屬性はケースに敏感です。 1.バックグラウンドカラーや背景色など、セレクターと屬性名はケース非感受性です。 2。値の16進(jìn)數(shù)色は大文字と小文字を區(qū)別しますが、赤と赤などの名前の色は無(wú)効です。 3. URLは癥例に敏感であり、ファイルロードの問(wèn)題を引き起こす可能性があります。 4.カスタムプロパティ(変數(shù))はケースに敏感であり、使用する場(chǎng)合はケースの一貫性に注意を払う必要があります。

conic-gradient()関數(shù)とは何ですか? conic-gradient()関數(shù)とは何ですか? Jul 01, 2025 am 01:16 AM

TheConic-Gradient()functionsscreateScular勾配の勾配は、測(cè)定されていることを確認(rèn)してください

See all articles