1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
// src/store/modules/user.js
const state = {
user: null,
isLoggedIn: false
}
const mutations = {
SET_USER(state, user) {
state.user = user
state.isLoggedIn = !!user
}
}
const actions = {
login({ commit }, credentials) {
// 模拟登录API调用
return new Promise((resolve) => {
setTimeout(() => {
const user = {
id: 1,
username: credentials.username,
email: `${credentials.username}@example.com`
}
// 保存到localStorage
localStorage.setItem('user', JSON.stringify(user))
commit('SET_USER', user)
resolve(user)
}, 500)
})
},
logout({ commit }) {
localStorage.removeItem('user')
commit('SET_USER', null)
},
checkAuth({ commit }) {
const user = localStorage.getItem('user')
if (user) {
commit('SET_USER', JSON.parse(user))
}
}
}
const getters = {
currentUser: state => state.user,
isLoggedIn: state => state.isLoggedIn
}
export default {
namespaced: true,
state,
mutations,
actions,
getters
}
// src/store/modules/posts.js
const state = {
posts: [],
currentPost: null,
loading: false,
error: null
}
const mutations = {
SET_POSTS(state, posts) {
state.posts = posts
},
SET_CURRENT_POST(state, post) {
state.currentPost = post
},
ADD_POST(state, post) {
state.posts.unshift(post)
},
UPDATE_POST(state, updatedPost) {
const index = state.posts.findIndex(p => p.id === updatedPost.id)
if (index !== -1) {
state.posts.splice(index, 1, updatedPost)
}
if (state.currentPost && state.currentPost.id === updatedPost.id) {
state.currentPost = updatedPost
}
},
DELETE_POST(state, postId) {
state.posts = state.posts.filter(p => p.id !== postId)
},
SET_LOADING(state, status) {
state.loading = status
},
SET_ERROR(state, error) {
state.error = error
}
}
const actions = {
fetchPosts({ commit }) {
commit('SET_LOADING', true)
// 模拟API调用
return new Promise((resolve) => {
setTimeout(() => {
const posts = [
{
id: 1,
title: 'Vue.js Basics',
content: 'Vue.js is a progressive JavaScript framework...',
author: 'John Doe',
createdAt: '2023-04-10T10:00:00Z',
comments: []
},
{
id: 2,
title: 'Vuex for State Management',
content: 'Vuex is the official state management library for Vue...',
author: 'Jane Smith',
createdAt: '2023-04-11T14:30:00Z',
comments: []
}
]
commit('SET_POSTS', posts)
commit('SET_LOADING', false)
resolve(posts)
}, 800)
})
},
fetchPostById({ commit, state }, postId) {
// 如果已经有这个帖子,直接返回
const existingPost = state.posts.find(p => p.id === parseInt(postId))
if (existingPost) {
commit('SET_CURRENT_POST', existingPost)
return Promise.resolve(existingPost)
}
// 否则模拟API调用
commit('SET_LOADING', true)
return new Promise((resolve) => {
setTimeout(() => {
const post = {
id: parseInt(postId),
title: `Post ${postId}`,
content: `Content for post ${postId}...`,
author: 'Author Name',
createdAt: new Date().toISOString(),
comments: []
}
commit('SET_CURRENT_POST', post)
commit('SET_LOADING', false)
resolve(post)
}, 500)
})
}
}
const getters = {
allPosts: state => state.posts,
currentPost: state => state.currentPost,
isLoading: state => state.loading
}
export default {
namespaced: true,
state,
mutations,
actions,
getters
}
// src/store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import posts from './modules/posts'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
user,
posts
}
})
|