← Back to Vue.js Course | Chapter 7: Vue Router | Lesson 3 of 7

router-link का उपयोग

router-link navigation links बनाता है जो page reload किए बिना URL बदलते हैं।

In this page:

  1. router-link
Syntax
markup
<router-link to="/path">link text</router-link>
<router-link :to="{ name: 'route-name' }">link text</router-link>

router-link

<a href> की बजाय <router-link to="/about"> इस्तेमाल करें। यह एक anchor render करता है, automatically active classes add करता है, और client-side navigation के लिए clicks intercept करता है। to prop एक path string या name और params वाला एक object accept करता है।

Note: router-link-active class current route से match होने वाले links mark करती है।

उदाहरण: router-link

markup
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  <script src="https://unpkg.com/vue-router@4/dist/vue-router.global.js"></script>
</head>
<body>
<div id="app">
  <nav>
    <router-link to="/">Home</router-link> |
    <router-link :to="{ name: 'about' }">About</router-link>
  </nav>
  <router-view></router-view>
</div>
<script>
  const router = VueRouter.createRouter({
    history: VueRouter.createWebHashHistory(),
    routes: [
      { path: "/", component: { template: "<p>Home content</p>" } },
      { path: "/about", name: "about", component: { template: "<p>About content</p>" } },
    ],
  });
  Vue.createApp({}).use(router).mount("#app");
  router.isReady().then(async () => {
    document.querySelectorAll("a")[1].click();
    await router.isReady(); await Vue.nextTick(); await new Promise((r) => setTimeout(r, 30));
    console.log("path:", router.currentRoute.value.path, "| view:", document.querySelector("p").textContent);
  });
</script>
</body>
</html>

<!-- Output:
Rendered: Home | AboutAbout content
console: path: /about | view: About content
-->
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. plain anchors इस्तेमाल करना जो page reload करते हैं
  2. to prop भूल जाना
  3. names की बजाय paths hard-code करना
चैप्टर सारांश
  • router-link client-side navigate करता है
  • to एक path या object accept करता है
  • Active classes automatically add होती हैं
  • पूरे page reloads से बचता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.