01、main.js程式碼如下:
// 引入createApp用於建立Vue例項 import {createApp} from 'vue' // 引入App.vue根元件 import App from './App.vue' //第一步:引入pinia import {createPinia} from 'pinia' const app = createApp(App); //第二步:建立pinia例項 const pinia = createPinia() //第三步:將pinia例項掛載到Vue例項上 app.use(pinia) // App.vue的根元素id為app app.mount('#app')
02、App.vue程式碼如下:
<template> <div class="app"> <h2 class="title">App.Vue</h2> <Page1/> <br> <Page2/> </div> </template> <script lang="ts" setup name="App"> import Page1 from "@/view/Page1.vue"; import Page2 from "@/view/Page2.vue"; </script> <style scoped> .app { background-color: #ddd; box-shadow: 0 0 10px; border-radius: 10px; padding: 20px; } .nav-button { display: inline-block; /* 讓連結顯示為塊級元素,以便應用寬度和高度 */ padding: 10px 20px; /* 內邊距 */ margin: 0 5px; /* 外邊距,用於按鈕之間的間隔 */ text-decoration: none; /* 移除下劃線 */ color: white; /* 文字顏色 */ background-color: #007bff; /* 背景顏色 */ border-radius: 5px; /* 邊框圓角 */ transition: background-color 0.3s; /* 平滑過渡效果 */ } .nav-button:hover { background-color: #0056b3; /* 滑鼠懸停時的背景顏色 */ } .nav-button.router-link-active { background-color: #28a745; /* 當前啟用(路由匹配)時的背景顏色 */ } .mai-content { /* 新增邊框樣式 */ border: 2px solid #000; /* 邊框寬度、樣式和顏色 */ border-radius: 5px; /* 可選:新增邊框圓角 */ padding: 20px; /* 可選:給內部內容新增一些內邊距 */ margin: 20px; /* 可選:給元素新增一些外邊距,以便與其他元素隔開 */ } </style>
03、Page1.vue程式碼如下:
<template> <div class="mypage"> <h2>當前求和為:{{ countStore.sum }}</h2> <h2>姓名:{{ countStore.name }} , 年齡{{ countStore.age }}</h2> <select v-model="n"> <option :value="1">1</option> <option :value="2">2</option> <option :value="3">3</option> </select> <button @click="add">加</button> <button @click="changeData">修改</button> </div> </template> <script lang="ts" name="page1" setup> import {ref} from "vue"; import {useCountStore} from "@/store/count"; const countStore = useCountStore(); let sum = ref(1); let n = ref(1); function add() { // countStore.sum += n.value; //第三種方法,呼叫函式控制增長和邏輯 countStore.myAdd(n.value); } function changeData() { countStore.$patch({ sum: 888, name: "李四", age: 22 }) } </script> <style scoped> .mypage { background-color: #ddd; box-shadow: 0 0 10px; border-radius: 10px; padding: 20px; button { margin: 0 5px; } } </style>
04、Page2.vue程式碼如下:
<template> <div class="mypage"> <ul> <li v-for="talk in myTalkStore.talklist" :key="talk.id">{{ talk.id }} -- {{ talk.title }} </li> <br> <li v-for="talk in myTalkStore.$state.talklist" :key="talk.id">{{ talk.id }} -- {{ talk.title }} </li> </ul> </div> </template> <script lang="ts" name="page2" setup> import {useCountStore} from "@/store/count"; const myTalkStore = useCountStore(); </script> <style scoped> .mypage { background-color: #ddd; box-shadow: 0 0 10px; border-radius: 10px; padding: 20px; button { margin: 0 5px; } } </style>
05、count.ts程式碼如下:
import {defineStore} from 'pinia' export const useCountStore = defineStore('count', { actions: { //定義一個函式,可以在函式控制增長和邏輯 myAdd(value: number) { if (this.sum < 10) { this.sum += value; } } }, //真正儲存資料的地方 state() { return { sum: 6, name: '張三', age: 18, talklist: [ {id: 'a001', title: '1今天你想我了沒'}, {id: 'a002', title: '2今天你想我了沒'}, {id: 'a003', title: '3今天你想我了沒'}, ] } } })
06、介面如圖
07、瀏覽器效果如下: