Skip to content

Commit c79cb4d

Browse files
committed
feat: update pinia
1 parent 5108a1a commit c79cb4d

File tree

12 files changed

+168
-84
lines changed

12 files changed

+168
-84
lines changed

package-lock.json

Lines changed: 88 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"js-cookie": "^2.2.1",
2727
"markdown-it": "^12.0.4",
2828
"nprogress": "^0.2.0",
29-
"vue": "^3.1.4",
29+
"pinia": "^2.0.0-rc.9",
30+
"vue": "^3.2.0",
3031
"vue-global-api": "^0.2.1",
3132
"vue-router": "^4.0.3",
3233
"vue-toastification": "^2.0.0-rc.1",

src/App.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
<script lang="ts" setup>
66
import { useHead } from "@vueuse/head";
77
import { POSITION, useToast } from "vue-toastification";
8-
import { useStore } from "vuex";
8+
import { useStore } from "~/store/pinia";
99
import { useRoute } from "vue-router";
1010
1111
import GithubStar from "~/components/common/GithubStar.vue";
12-
import { ACTIONS } from "~/store/type";
1312
import { useBaiduInit } from "./utils/pageView";
1413
const store = useStore();
1514
const toast = useToast();
@@ -36,9 +35,10 @@ onMounted(async () => {
3635
3736
// 应用初始化 Store 数据
3837
// 用户登录信息
39-
store.dispatch(ACTIONS.GET_AUTHED_USER_INFO);
38+
store.GetAuthedUserInfo();
39+
4040
// 用户主题信息
41-
store.dispatch(ACTIONS.GET_CATEGORY_LIST);
41+
store.GetCategoryList();
4242
// 高亮初始化
4343
(window as any)?.hljs?.initHighlightingOnLoad?.();
4444
});

src/components/article/Comment.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script lang="ts" setup>
22
import { useRouter } from "vue-router";
33
import { useToast } from "vue-toastification";
4-
import { useStore } from "vuex";
5-
import type { StoreType } from "~/store/index";
4+
import { useStore } from "~/store/pinia";
5+
66
import { GetCommentList, PostComment } from "~/api/article";
77
import { DefaultFormat } from "~/utils/time";
88
@@ -13,9 +13,9 @@ const props = defineProps({
1313
1414
const toast = useToast();
1515
const router = useRouter();
16-
const store = useStore<StoreType>();
16+
const store = useStore();
1717
18-
const isAuthed = computed(() => store?.state?.User?.Id ?? 0 > 0);
18+
const isAuthed = computed(() => store?.User?.Id ?? 0 > 0);
1919
2020
// 评论值
2121
// ref: Comment = ""

src/components/common/Header.vue

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
</template>
2222
<script lang="ts" setup>
2323
import { useRouter } from "vue-router";
24-
import { useStore } from "vuex";
25-
import type { StoreType } from "~/store";
24+
import { useStore } from "~/store/pinia";
2625
2726
const router = useRouter();
2827
@@ -40,15 +39,15 @@ const ToIndex = () => {
4039
router.push("/");
4140
// window.location.href = "/"
4241
};
43-
const store = useStore<StoreType>();
42+
const store = useStore();
4443
// 判断用户是否作者
4544
const isAuthor = computed(
46-
() => !!store.state.User?.Roles?.find(i => i.Title == "Author")
45+
() => !!store.User?.Roles?.find(i => i.Title == "Author")
4746
);
4847
// 获取用户ID
49-
const userId = computed(() => store.state.User.Id ?? 0);
48+
const userId = computed(() => store.User.Id ?? 0);
5049
// 获取用户头像
51-
const avatar = computed(() => store.state.User.Avatar ?? "");
50+
const avatar = computed(() => store.User.Avatar ?? "");
5251
// props
5352
const title = props.title;
5453
const brief = props.brief;

src/components/layouts/Content/index.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import { useStore } from "vuex";
2-
3-
import { StoreType } from "~/store";
1+
import { useStore } from "~/store/pinia";
42

53
import "./index.css";
64
const DefaultLayout = defineComponent({
75
name: "content-layout",
86
setup() {
9-
const store = useStore<StoreType>();
7+
const store = useStore();
108
return {
11-
CategoryList: computed(() => store.state.Article.CategoryList),
9+
CategoryList: computed(() => store.Article.CategoryList),
1210
};
1311
},
1412
render() {

src/components/layouts/Content/index.vue

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@
2323
</template>
2424

2525
<script lang="ts" setup>
26-
import { useStore } from "vuex";
26+
import { useStore } from "~/store/pinia";
2727
28-
import type { StoreType } from "~/store";
28+
const store = useStore();
2929
30-
const store = useStore<StoreType>();
31-
32-
const CategoryList = computed(() => store.state.Article.CategoryList);
30+
const CategoryList = computed(() => store.Article.CategoryList);
3331
</script>
3432

3533
<style scoped>

src/main.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createApp } from "vue";
22
import { createHead } from "@vueuse/head";
3+
import { createPinia } from "pinia";
34
import "vue-global-api";
45
import Toast, { PluginOptions, POSITION } from "vue-toastification";
56
import "vue-toastification/dist/index.css";
@@ -22,7 +23,17 @@ const options: PluginOptions = {
2223
transition: "Vue-Toastification__fade",
2324
};
2425
const head = createHead();
26+
const pinia = createPinia();
27+
pinia.use(({ store }) => {
28+
store.$subscribe(t => {
29+
console.log("store state changed", t);
30+
});
31+
store.$onAction(a => {
32+
console.log("store action fired", a);
33+
});
34+
});
2535
createApp(App)
36+
.use(pinia)
2637
.use(Toast, options)
2738
.use(head)
2839
.use(RouterInstance)

src/store/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export type StoreType = {
1111
};
1212
};
1313

14+
/**
15+
* @deprecated 废弃
16+
*/
1417
const Store = createStore<StoreType>({
1518
state() {
1619
return {

0 commit comments

Comments
 (0)