Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
夜猫逐梦
MyOpen
提交
b86b7e3a
M
MyOpen
项目概览
夜猫逐梦
/
MyOpen
通知
2
Star
0
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
MyOpen
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
b86b7e3a
编写于
9月 12, 2023
作者:
S
sw_pc
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
多组件共享数据
上级
2f52f23e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
145 addition
and
35 deletion
+145
-35
course/vue2/vuex/heima/src/App.vue
course/vue2/vuex/heima/src/App.vue
+21
-9
course/vue2/vuex/heima/src/components/Count2.vue
course/vue2/vuex/heima/src/components/Count2.vue
+44
-0
course/vue2/vuex/heima/src/components/Person.vue
course/vue2/vuex/heima/src/components/Person.vue
+38
-0
course/vue2/vuex/heima/src/store/index.js
course/vue2/vuex/heima/src/store/index.js
+42
-26
未找到文件。
course/vue2/vuex/heima/src/App.vue
浏览文件 @
b86b7e3a
<
template
>
<div
id=
"app"
>
<img
alt=
"Vue logo"
src=
"./assets/logo.png"
>
<Count
msg=
"Welcome to Your Vue.js App"
/>
<HelloWorld
id=
"hello"
msg=
"Welcome to Your Vue.js App"
/>
<img
alt=
"Vue logo"
src=
"./assets/logo.png"
/>
<Count
class=
"hideComponents"
msg=
"Welcome to Your Vue.js App"
/>
<HelloWorld
id=
"hello"
msg=
"Welcome to Your Vue.js App"
/>
<hr
/>
<Count2
/>
<hr
/>
<Person
/>
</div>
</
template
>
<
script
>
import
HelloWorld
from
'
./components/HelloWorld.vue
'
import
Count
from
'
./components/Count.vue
'
import
HelloWorld
from
"
./components/HelloWorld.vue
"
;
import
Count
from
"
./components/Count.vue
"
;
import
Count2
from
"
./components/Count2
"
;
import
Person
from
"
./components/Person
"
;
export
default
{
name
:
'
App
'
,
name
:
"
App
"
,
components
:
{
HelloWorld
,
Count
}
}
Count
,
Count2
,
Person
,
},
};
</
script
>
<
style
>
...
...
@@ -32,4 +40,8 @@ export default {
#hello
{
display
:
none
;
}
.hideComponents
{
display
:
none
;
}
</
style
>
course/vue2/vuex/heima/src/components/Count2.vue
0 → 100644
浏览文件 @
b86b7e3a
<
template
>
<div>
<h1>
当前求和为:
{{
sum
}}
</h1>
<h3>
当前求和的10倍为:
{{
bigSum
}}
</h3>
<h3>
我是
{{
name
}}
,我在
{{
school
}}
学习
</h3>
<h3
style=
"color: red"
>
Person组件的总人数是:
{{
personList
.
length
}}
</h3>
<select
v-model.number=
"n"
>
<option
value=
"1"
>
1
</option>
<option
value=
"2"
>
2
</option>
<option
value=
"3"
>
3
</option>
</select>
<button
@
click=
"increment(n)"
>
+
</button>
<button
@
click=
"decrement(n)"
>
-
</button>
<button
@
click=
"incrementOdd(n)"
>
当前求和为奇数再加
</button>
<button
@
click=
"incrementWait(n)"
>
等一等再加
</button>
</div>
</
template
>
<
script
>
import
{
mapState
,
mapGetters
,
mapMutations
,
mapActions
}
from
'
vuex
'
export
default
{
name
:
'
Count2Com
'
,
data
()
{
return
{
n
:
1
,
//用户选择的数字
}
},
methods
:
{
...
mapMutations
({
increment
:
'
ADD
'
,
decrement
:
'
SUBTRACT
'
}),
...
mapActions
({
incrementOdd
:
'
addOdd
'
,
incrementWait
:
'
addWait
'
})
},
computed
:{
...
mapState
([
'
sum
'
,
'
school
'
,
'
name
'
,
'
personList
'
]),
...
mapGetters
([
'
bigSum
'
])
}
}
</
script
>
<
style
>
button
{
margin-left
:
5px
;
}
</
style
>
course/vue2/vuex/heima/src/components/Person.vue
0 → 100644
浏览文件 @
b86b7e3a
<
template
>
<div>
<h1>
人员列表
</h1>
<h3
style=
"color:red"
>
Count组件求和为:
{{
sum
}}
</h3>
<input
type=
"text"
placeholder=
"请输入名字"
v-model=
"name"
>
<button
@
click=
"add"
>
添加
</button>
<ul>
<li
v-for=
"p in personList"
:key=
"p.id"
>
{{
p
.
name
}}
</li>
</ul>
</div>
</
template
>
<
script
>
import
{
nanoid
}
from
'
nanoid
'
export
default
{
name
:
'
PersonComp
'
,
data
()
{
return
{
name
:
''
}
},
computed
:{
personList
(){
return
this
.
$store
.
state
.
personList
},
sum
(){
return
this
.
$store
.
state
.
sum
}
},
methods
:
{
add
(){
const
personObj
=
{
id
:
nanoid
(),
name
:
this
.
name
}
this
.
$store
.
commit
(
'
ADD_PERSON
'
,
personObj
)
this
.
name
=
''
}
}
}
</
script
>
course/vue2/vuex/heima/src/store/index.js
浏览文件 @
b86b7e3a
//引入Vue核心库
import
Vue
from
'
vue
'
//引入Vuex
import
Vuex
from
'
vuex
'
//应用Vuex插件
Vue
.
use
(
Vuex
)
export
default
new
Vuex
.
Store
({
state
:
{
sum
:
0
//当前的和
},
getters
:
{
bigSum
(
state
)
{
console
.
log
(
state
)
return
state
.
sum
*
10
}
},
mutations
:
{
ADD
(
state
,
value
){
console
.
log
(
"
mutations ADD
"
)
state
.
sum
+=
value
},
SUBTRACT
(
state
,
value
){
console
.
log
(
"
mutations SUBTRACT
"
)
state
.
sum
-=
value
}
},
actions
:
{
//准备actions对象——响应组件中用户的动作
const
actions
=
{
addOdd
(
context
,
value
){
console
.
log
(
"
actions中的addOdd被调用了
"
)
if
(
context
.
state
.
sum
%
2
){
...
...
@@ -36,7 +19,40 @@ export default new Vuex.Store({
context
.
commit
(
'
ADD
'
,
value
)
},
500
)
},
},
modules
:
{
}
}
//准备mutations对象——修改state中的数据
const
mutations
=
{
ADD
(
state
,
value
){
state
.
sum
+=
value
},
SUBTRACT
(
state
,
value
){
state
.
sum
-=
value
},
ADD_PERSON
(
state
,
value
){
console
.
log
(
'
mutations中的ADD_PERSON被调用了
'
)
state
.
personList
.
unshift
(
value
)
}
}
//准备state对象——保存具体的数据
const
state
=
{
sum
:
0
,
//当前的和
name
:
'
JOJO
'
,
school
:
'
尚硅谷
'
,
personList
:[
{
id
:
'
001
'
,
name
:
'
JOJO
'
}
]
}
//准备getters对象——用于将state中的数据进行加工
const
getters
=
{
bigSum
(){
return
state
.
sum
*
10
}
}
//创建并暴露store
export
default
new
Vuex
.
Store
({
actions
,
mutations
,
state
,
getters
})
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录