← Back to Vue.js Course | Chapter 8: State Management | Lesson 1 of 6

Pinia क्या है

Pinia Vue का official store है: state रखने की एक central जगह जो कई components share करते हैं।

Pinia क्या है

जब कई components को same data चाहिए, कई layers के through props pass करना painful हो जाता है। एक Pinia store उस state को एक जगह hold करता है, और components इसे सीधे पढ़ते और update करते हैं।

Pinia ने recommended library की तरह Vuex की जगह ली, एक simpler API, TypeScript support, और DevTools integration के साथ।

Note: Pinia Vue 3 के लिए Vuex का official successor है।

उदाहरण: What is Pinia

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/[email protected]/lib/index.iife.js"></script>
  <script src="https://unpkg.com/pinia@2/dist/pinia.iife.js"></script>
</head>
<body>
<div id="app">
  <p>Header sees: {{ counter.count }}</p>
  <p>Footer sees: {{ counter.count }}</p>
  <button @click="counter.count++">Increment shared count</button>
</div>
<script>
  const useCounter = Pinia.defineStore("counter", { state: () => ({ count: 0 }) });
  const app = Vue.createApp({ setup() { return { counter: useCounter() }; } });
  app.use(Pinia.createPinia());
  app.mount("#app");
  document.querySelector("button").click();
  Vue.nextTick(() => console.log(document.querySelector("#app").textContent.replace(/\s+/g, " ").trim()));
</script>
</body>
</html>

<!-- Output:
Rendered: Header sees: 1Footer sees: 1Increment shared count
console: Header sees: 1Footer sees: 1Increment shared count
-->
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. बेवजह सारी state को एक store में डालना
  2. नए Vue 3 projects के लिए Vuex इस्तेमाल करना
  3. बिना care के store के बाहर state mutate करना
चैप्टर सारांश
  • एक store shared reactive state है
  • Pinia official store है
  • इसने Vuex की जगह ली
  • Cross-component data के लिए great
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.