Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
瓜皮233
Vue-购物车案例
提交
9f474671
V
Vue-购物车案例
项目概览
瓜皮233
/
Vue-购物车案例
与 Fork 源项目一致
Fork自
inscode / VueJS
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
Vue-购物车案例
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
9f474671
编写于
4月 30, 2023
作者:
6
642b59b229c89a1824b83ed8
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
UPDATE
上级
338857c3
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
107 addition
and
23 deletion
+107
-23
package.json
package.json
+1
-1
src/App.vue
src/App.vue
+46
-15
src/components/Counter/Counter.vue
src/components/Counter/Counter.vue
+23
-3
src/components/Footer/Footer.vue
src/components/Footer/Footer.vue
+20
-3
src/components/Goods/Goods.vue
src/components/Goods/Goods.vue
+14
-1
src/components/eventBus.js
src/components/eventBus.js
+3
-0
未找到文件。
package.json
浏览文件 @
9f474671
...
...
@@ -9,7 +9,7 @@
"dependencies"
:
{
"
axios
"
:
"
^1.4.0
"
,
"
guess
"
:
"
^1.0.2
"
,
"
vue
"
:
"
^3.2.
3
7
"
"
vue
"
:
"
^3.2.
4
7
"
},
"devDependencies"
:
{
"
@vitejs/plugin-vue
"
:
"
^3.0.1
"
,
...
...
src/App.vue
浏览文件 @
9f474671
...
...
@@ -7,9 +7,12 @@
<!--
<h1>
App 根组件
</h1>
-->
<!-- 商品列表 -->
<Goods
v-for=
"item in list"
:key=
"item.id"
:title=
"item.goods_name"
:pic=
"item.goods_img"
:chstate=
"item.goods_state"
:id=
"item.id"
@
good_check_change=
"updateGoodState"
></Goods>
:id=
"item.id"
:price=
"item.goods_price"
:count=
"item.goods_count"
@
good_check_change=
"updateGoodState"
></Goods>
<Footer></Footer>
<Footer
:full_checked=
"allChecked"
:amount=
"amt"
@
changAllGoodsState=
"changeState"
></Footer>
<div>
eventBus
</div>
</div>
</
template
>
...
...
@@ -18,11 +21,12 @@ import axios from 'axios' // 导入 axios 模块
import
Header
from
'
@/components/Header/Header.vue
'
// 导入 Header 组件
import
Goods
from
'
@/components/Goods/Goods.vue
'
// 导入 Goods 组件
import
Footer
from
'
@/components/Footer/Footer.vue
'
import
eventBus
from
'
./components/eventBus.js
'
export
default
{
components
:
{
Header
,
// 注册 Header 组件
Goods
,
// 注册 Goods 组件
Footer
,
},
data
()
{
return
{
...
...
@@ -32,28 +36,55 @@ export default {
},
methods
:
{
// 初始化购物车的方法,通过axios发送get请求获取数据
async
initBuyCars
()
{
// 通过 axios 请求数据
const
{
data
:
res
}
=
await
axios
.
get
(
'
https://www.escook.cn/api/cart
'
)
if
(
res
.
status
===
200
)
{
// 请求成功,将数据赋值给 list
this
.
list
=
res
.
list
// 发送异步请求获取购物车数据
const
{
data
:
res
}
=
await
axios
.
get
(
'
https://www.escook.cn/api/cart
'
);
// 判断请求是否成功
if
(
res
.
status
===
200
)
{
// 将获取到的数据赋值给组件数据的 list 属性
this
.
list
=
res
.
list
;
}
},
updateGoodState
(
e
){
this
.
list
.
some
(
item
=>
{
if
(
item
.
id
===
e
.
id
)
{
item
.
goods_state
=
e
.
state
return
true
// 更新商品状态的方法,接收一个对象作为参数
updateGoodState
(
e
)
{
// 遍历组件数据的 list 数组
this
.
list
.
some
(
item
=>
{
// 如果当前遍历到的 item 对象的 id 和传入的参数 e 中的 id 相等
if
(
item
.
id
===
e
.
id
)
{
// 将当前遍历到的 item 对象的 goods_state 属性更新为传入的参数 e 中的 state 值
item
.
goods_state
=
e
.
state
;
// 返回 true 结束循环
return
true
;
}
})
});
},
changeState
(
val
){
this
.
list
.
forEach
(
item
=>
item
.
goods_state
=
val
.
value
)
}
},
computed
:{
allChecked
(){
return
this
.
list
.
every
(
item
=>
item
.
goods_state
)
},
amt
(){
return
this
.
list
.
filter
(
item
=>
item
.
goods_state
).
reduce
((
t
,
item
)
=>
t
+=
item
.
goods_price
*
item
.
goods_count
,
0
)
}
},
created
()
{
// 在实例创建之后请求数据
this
.
initBuyCars
()
}
}
</
script
>
...
...
src/components/Counter/Counter.vue
浏览文件 @
9f474671
...
...
@@ -3,14 +3,34 @@
<!-- 减 1 的按钮 -->
<button
type=
"button"
class=
"btn btn-light btn-sm"
>
-
</button>
<!-- 购买的数量 -->
<span
class=
"number-box"
>
1
</span>
<span
class=
"number-box"
>
{{
num
}}
</span>
<!-- 加 1 的按钮 -->
<button
type=
"button"
class=
"btn btn-light btn-sm"
>
+
</button>
<button
type=
"button"
class=
"btn btn-light btn-sm"
@
click=
"add"
>
+
</button>
</div>
</
template
>
<
script
>
export
default
{}
import
bus
from
'
../eventBus.js
'
;
export
default
{
props
:{
num
:{
type
:
Number
,
default
:
1
},
count_id
:{
type
:
Number
,
// 指定 id 只接受数字类型
required
:
true
,
// 指定 id 是必须的,必须传递 id 属性
}
},
methods
:{
add
(){
}
},
}
</
script
>
<
style
lang=
"less"
scoped
>
...
...
src/components/Footer/Footer.vue
浏览文件 @
9f474671
...
...
@@ -2,14 +2,14 @@
<div
class=
"footer-container"
>
<!-- 左侧的全选 -->
<div
class=
"custom-control custom-checkbox"
>
<input
type=
"checkbox"
class=
"custom-control-input"
id=
"cbFull"
:checked=
"
true"
/>
<input
type=
"checkbox"
class=
"custom-control-input"
id=
"cbFull"
:checked=
"
full_checked"
@
change=
"changAppListGoodsSState"
/>
<label
class=
"custom-control-label"
for=
"cbFull"
>
全选
</label>
</div>
<!-- 中间的合计 -->
<div>
<span>
合计:
</span>
<span
class=
"total-price"
>
¥
{{
0
}}
</span>
<span
class=
"total-price"
>
¥
{{
amount
.
toFixed
(
2
)
}}
</span>
</div>
<!-- 结算按钮 -->
...
...
@@ -18,7 +18,24 @@
</
template
>
<
script
>
export
default
{}
import
bus
from
'
../eventBus
'
;
export
default
{
props
:{
full_checked
:{
default
:
true
,
type
:
Boolean
,
},
amount
:{
type
:
Number
,
default
:
0
}
},
methods
:{
changAppListGoodsSState
(
e
){
this
.
$emit
(
'
changAllGoodsState
'
,{
value
:
e
.
target
.
checked
})
}
}
}
</
script
>
<
style
lang=
"less"
scoped
>
...
...
src/components/Goods/Goods.vue
浏览文件 @
9f474671
...
...
@@ -18,14 +18,19 @@
<h6
class=
"goods-title"
>
{{
title
}}
</h6>
<div
class=
"goods-info-bottom"
>
<!-- 商品价格 -->
<span
class=
"goods-price"
>
¥0
</span>
<span
class=
"goods-price"
>
¥
{{
price
}}
</span>
<Counter
:num=
"count"
:count_id=
"id"
></Counter>
</div>
</div>
</div>
</
template
>
<
script
>
import
Counter
from
'
@/components/Counter/Counter.vue
'
export
default
{
components
:{
Counter
,
},
props
:
{
// 商品标题
title
:
{
...
...
@@ -46,6 +51,14 @@ export default {
id
:
{
type
:
Number
,
// 指定 id 只接受数字类型
required
:
true
,
// 指定 id 是必须的,必须传递 id 属性
},
price
:{
type
:
Number
,
required
:
true
,
},
count
:{
type
:
Number
,
default
:
1
}
},
methods
:
{
...
...
src/components/eventBus.js
0 → 100644
浏览文件 @
9f474671
const
eventBus
=
'
hello
'
export
default
eventBus
;
// 导出 EventBus 对象
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录