فهرست سرفصل‌های Vue
خانه (HOME) معرفی (Intro) دایرکتیوها (Directives) v-bind (v-bind) v-if (v-if) v-show (v-show) v-for (v-for) رویدادها (Events) v-on (v-on) متدها (Methods) تغییردهنده های رویداد (Event Modifiers) فرم ها (Forms) v-model (v-model) بایندینگ CSS (CSS Binding) ویژگی های محاسبه شده (Computed Properties) واچرها (Watchers) قالب ها (Templates) چرا، چگونه و راه اندازی (Why, How and Setup) اولین صفحه SFC (First SFC Page) کامپوننت ها (Components) پراپس (Props) کامپوننت های v-for (v-for Components) $emit() ($emit()) ویژگی های عبوری (Fallthrough) (Fallthrough Attributes) استایل Scoped (Scoped Styling) کامپوننت های محلی (Local Components) اسلات ها (Slots) v-slot (v-slot) اسلات های Scoped (Scoped Slots) کامپوننت های پویا (Dynamic Components) Teleport (Teleport) درخواست HTTP (HTTP Request) رفرنس های تمپلیت (Template Refs) هوک های چرخه عمر (Lifecycle Hooks) Provide/Inject (Provide/Inject) مسیریابی (Routing) ورودی های فرم (Form Inputs) انیمیشن ها (Animations) انیمیشن با v-for (Animations with v-for) بیلد (Build) Composition API (Composition API) ویژگی های توکار (Built-in Attributes) ویژگی 'is' ('is' Attribute) ویژگی 'key' ('key' Attribute) ویژگی 'ref' ('ref' Attribute) کامپوننت های توکار (Built-in Components) <KeepAlive> (<KeepAlive>) <Teleport> (<Teleport>) <Transition> (<Transition>) <TransitionGroup> (<TransitionGroup>) المان های توکار (Built-in Elements) <component> (<component>) <slot> (<slot>) <template> (<template>) نمونه کامپوننت (Component Instance) $attrs ($attrs) $data ($data) $el ($el) $parent ($parent) $props ($props) $refs ($refs) $root ($root) $slots ($slots) $emit() ($emit()) $forceUpdate() ($forceUpdate()) $nextTick() ($nextTick()) $watch() ($watch()) دایرکتیوها (Directives) v-bind (v-bind) v-cloak (v-cloak) v-for (v-for) v-html (v-html) v-if (v-if) v-else-if (v-else-if) v-else (v-else) v-memo (v-memo) v-model (v-model) v-on (v-on) v-once (v-once) v-pre (v-pre) v-show (v-show) v-slot (v-slot) v-text (v-text) گزینه های نمونه (Instance Options) داده ها (data) متدها (methods) محاسبه شده ها (computed) watch (watch) پراپس (props) emits (emits) expose (expose) هوک های چرخه عمر (Lifecycle Hooks) beforeCreate (beforeCreate) created (created) beforeMount (beforeMount) mounted (mounted) beforeUpdate (beforeUpdate) updated (updated) beforeUnmount (beforeUnmount) unmounted (unmounted) errorCaptured (errorCaptured) renderTracked (renderTracked) renderTriggered (renderTriggered) activated (activated) deactivated (deactivated) serverPrefetch (serverPrefetch) مثال ها (Examples) تمرین ها (Exercises) کوییز (Quiz) سیلابس (Syllabus) برنامه مطالعه (Study Plan) سرور (Server) گواهینامه (Certificate)
نتیجه‌ای برای جستجو یافت نشد.
Vue

Vue — رفرنس های تمپلیت (Template Refs)

آخرین بروزرسانی: 1404/08/21

رفرنس های تمپلیت (Template Refs)

«رفرنس تمپلیت (Template Ref)» یعنی آدرس دهی مستقیم به یک عنصر DOM. وقتی روی تگ HTML ویژگی ref می گذاری، آن عنصر بعداً از طریق شیء $refs در دسترس است. مثل صدا زدن هم کلاسی با اسمش!

رفرنس ها و شیء $refs

با ref روی تگ ها، همان عناصر داخل $refs ذخیره می شوند. سپس می توانی از داخل اسکریپت به آن ها دسترسی بگیری و مقدارشان را تغییر دهی.

<template>
  <h1>Example</h1>
  <p>Click the button to put \"Hello!\" as the text in the green p element.</p>
  <button @click="changeVal">Change Text</button>
  <p ref="pEl">This is the initial text</p>
</template>

<script>
export default {
  methods: {
    changeVal() {
      this.$refs.pEl.innerHTML = "Hello!";
    }
  }
};
<\/script>

مشاهده در ادیتور

کپی متن با Template Refs

می خواهی متن از یک پاراگراف به دیگری برود؟ به سادگی با $refs مقدار را جابه جا کن.

<template>
  <h1>Example</h1>
  <p ref="p1">Click the button to copy this text into the paragraph below.</p>
  <button @click="transferText">Transfer text</button>
  <p ref="p2">...</p>
</template>

<script>
export default {
  methods: {
    transferText() {
      this.$refs.p2.innerHTML = this.$refs.p1.innerHTML;
    }
  }
};
<\/script>

مشاهده در ادیتور

گرفتن مقدار ورودی با $refs

می توانی به ویژگی های داخلی عنصر برسی. مثلاً مقدار input را بخوانی و در پاراگراف بنویسی.

<template>
  <h1>Example</h1>
  <p>Start writing inside the input element, and the text will be copied into the last paragraph by the use of the '$refs' object.</p>
  <input ref="inputEl" @input="getRefs" placeholder="Write something..">
  <p ref="pEl"></p>
</template>

<script>
export default {
  methods: {
    getRefs() {
      this.$refs.pEl.innerHTML = this.$refs.inputEl.value;
    }
  }
};
<\/script>

مشاهده در ادیتور

Template Ref همراه با v-for

اگر روی آیتم های v-for، ref بگذاری، داخل $refs یک آرایه می گیری. سپس عنصر دلخواه را با اندیس بردار.

<template>
  <h1>Example</h1>
  <p>Click the button to reveal the 3rd list element stored as an array element in the $refs object.</p>
  <button @click="getValue">Get the 3rd list element</button><br>
  <ul>
    <li v-for="x in liTexts" ref="liEl">{{ x }}</li>
  </ul>
  <pre>{{ thirdEl }}</pre>
</template>

<script>
export default {
  data() {
    return {
      thirdEl: " ",
      liTexts: ["Apple", "Banana", "Kiwi", "Tomato", "Lichi"]
    };
  },
  methods: {
    getValue() {
      this.thirdEl = this.$refs.liEl[2].innerHTML;
      console.log("this.$refs.liEl = ", this.$refs.liEl);
    }
  }
};
<\/script>

<style>
pre {
  background-color: lightgreen;
  display: inline-block;
}
<\/style>

مشاهده در ادیتور

گام های عملی رفرنس های تمپلیت

  1. روی تگ، یک نام ref بگذار.
  2. در اسکریپت، از this.$refs استفاده کن.
  3. ویژگی یا متن عنصر را تغییر بده.
  4. با v-for، آرایه رفرنس ها را مدیریت کن.

نکته: از رفرنس ها برای کارهای ضروری استفاده کن. بهتر است داده محور بمانی و فقط در موارد خاص سراغ DOM مستقیم بروی.

جمع بندی سریع

  • هر ref وارد $refs می شود.
  • می توانی متن یا ویژگی ها را تغییر دهی.
  • با v-for، آرایه رفرنس داری.
  • ساده نگه دار؛ اولویت با داده است.

مطالب مرتبط: درخواست HTTP، Teleport در Vue، و چرخه حیات کامپوننت ها. یکی از این ها را با کلیدواژه «رفرنس های تمپلیت» جستجو کن.