Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
眼红的小孩
添加商品
提交
041bd26e
添
添加商品
项目概览
眼红的小孩
/
添加商品
与 Fork 源项目一致
Fork自
inscode / VueJS
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
添
添加商品
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
041bd26e
编写于
4月 03, 2025
作者:
Q
qq_63480508
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Auto Commit
上级
aa50c4cb
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
206 addition
and
30 deletion
+206
-30
src/App.vue
src/App.vue
+2
-2
src/components/HelloWorld.vue
src/components/HelloWorld.vue
+204
-28
未找到文件。
src/App.vue
浏览文件 @
041bd26e
<
script
setup
>
import
HelloWorld
from
'
./components/HelloWorld.vue
'
import
TheWelcome
from
'
./components/TheWelcome.vue
'
//
import TheWelcome from './components/TheWelcome.vue'
</
script
>
<
template
>
<header>
<
img
alt=
"Vue logo"
class=
"logo"
src=
"./assets/logo.svg"
width=
"125"
height=
"125"
/
>
<
!--
<img
alt=
"Vue logo"
class=
"logo"
src=
"./assets/logo.svg"
width=
"125"
height=
"125"
/>
--
>
<div
class=
"wrapper"
>
<HelloWorld
msg=
"You did it!"
/>
...
...
src/components/HelloWorld.vue
浏览文件 @
041bd26e
<
script
setup
>
defineProps
({
msg
:
{
type
:
String
,
required
:
true
}
})
</
script
>
<
template
>
<div
class=
"greetings"
>
<h1
class=
"green"
>
{{
msg
}}
</h1>
<h3>
You’ve successfully created a project with
<a
target=
"_blank"
href=
"https://vitejs.dev/"
>
Vite
</a>
+
<a
target=
"_blank"
href=
"https://vuejs.org/"
>
Vue 3
</a>
.
</h3>
<div
class=
"container"
>
<h1>
添加商品
</h1>
<div
v-if=
"errorMessage"
class=
"error-message"
>
{{
errorMessage
}}
</div>
<div
class=
"add-product"
>
<input
v-model=
"newProduct.name"
placeholder=
"商品名称"
/>
<input
v-model.number=
"newProduct.price"
placeholder=
"单价"
type=
"number"
min=
"1"
step=
"1"
/>
<input
v-model.number=
"newProduct.quantity"
placeholder=
"数量"
type=
"number"
min=
"1"
step=
"1"
/>
<button
@
click=
"addProduct"
>
添加商品
</button>
</div>
<table
class=
"product-table"
v-if=
"products.length"
>
<thead>
<tr>
<th>
商品名称
</th>
<th>
单价
</th>
<th>
数量
</th>
<th>
小计
</th>
<th>
操作
</th>
</tr>
</thead>
<tbody>
<tr
v-for=
"(product, index) in products"
:key=
"index"
>
<td>
{{
product
.
name
}}
</td>
<td>
{{
product
.
price
.
toFixed
(
2
)
}}
</td>
<td>
<input
v-model.number=
"product.quantity"
type=
"number"
min=
"1"
step=
"1"
@
change=
"checkQuantity(index)"
/>
</td>
<td>
{{
(
product
.
price
*
product
.
quantity
).
toFixed
(
2
)
}}
</td>
<td>
<button
@
click=
"removeProduct(index)"
>
删除
</button>
</td>
</tr>
</tbody>
</table>
<div
v-else
class=
"empty-cart"
>
当前购物车为空
</div>
<div
class=
"total"
>
总计:
{{
totalPrice
.
toFixed
(
2
)
}}
元
</div>
</div>
</
template
>
<
script
setup
>
import
{
ref
,
computed
}
from
'
vue
'
;
const
products
=
ref
([]);
const
newProduct
=
ref
({
name
:
''
,
price
:
1
,
quantity
:
1
});
const
errorMessage
=
ref
(
''
);
const
addProduct
=
()
=>
{
if
(
newProduct
.
value
.
name
&&
newProduct
.
value
.
price
>=
1
&&
newProduct
.
value
.
quantity
>=
1
)
{
products
.
value
.
push
({
name
:
newProduct
.
value
.
name
,
price
:
newProduct
.
value
.
price
,
quantity
:
newProduct
.
value
.
quantity
});
newProduct
.
value
.
name
=
''
;
newProduct
.
value
.
price
=
1
;
newProduct
.
value
.
quantity
=
1
;
}
else
{
errorMessage
.
value
=
'
请输入有效的商品名称、单价和数量(数量至少为1)!
'
;
setTimeout
(()
=>
{
errorMessage
.
value
=
''
;
},
2000
);
}
console
.
log
(
errorMessage
.
value
);
};
const
removeProduct
=
(
index
)
=>
{
products
.
value
.
splice
(
index
,
1
);
};
const
checkQuantity
=
(
index
)
=>
{
if
(
products
.
value
[
index
].
quantity
<
1
)
{
products
.
value
[
index
].
quantity
=
1
;
}
};
const
totalPrice
=
computed
(()
=>
{
return
products
.
value
.
reduce
((
total
,
product
)
=>
{
return
total
+
product
.
price
*
product
.
quantity
;
},
0
);
});
</
script
>
<
style
scoped
>
.container
{
max-width
:
900px
;
margin
:
30px
auto
;
padding
:
30px
;
background
:
#ffffff
;
border-radius
:
10px
;
box-shadow
:
0
4px
8px
rgba
(
0
,
0
,
0
,
0.1
);
}
h1
{
font-weight
:
500
;
font-size
:
2.6rem
;
top
:
-10px
;
text-align
:
center
;
margin-bottom
:
25px
;
font-size
:
2.2em
;
color
:
#333
;
}
h3
{
font-size
:
1.2rem
;
.error-message
{
background-color
:
#ffdddd
;
border
:
1px
solid
#ff5c5c
;
color
:
#d8000c
;
padding
:
10px
;
border-radius
:
5px
;
text-align
:
center
;
margin-bottom
:
20px
;
}
.add-product
{
display
:
flex
;
flex-wrap
:
wrap
;
gap
:
15px
;
margin-bottom
:
25px
;
justify-content
:
center
;
}
.add-product
input
{
padding
:
8px
12px
;
font-size
:
1em
;
border
:
1px
solid
#ddd
;
border-radius
:
5px
;
flex
:
1
1
200px
;
}
.add-product
button
{
padding
:
8px
20px
;
font-size
:
1em
;
border
:
none
;
background-color
:
#42b983
;
color
:
#fff
;
border-radius
:
5px
;
cursor
:
pointer
;
transition
:
background-color
0.3s
;
}
.add-product
button
:hover
{
background-color
:
#369870
;
}
.product-table
{
width
:
100%
;
border-collapse
:
collapse
;
margin-bottom
:
25px
;
}
.greetings
h1
,
.greetings
h3
{
.product-table
th
,
.product-table
td
{
border
:
1px
solid
#ddd
;
padding
:
10px
;
text-align
:
center
;
}
@media
(
min-width
:
1024px
)
{
.greetings
h1
,
.greetings
h3
{
text-align
:
left
;
}
.product-table
th
{
background-color
:
#f7f7f7
;
font-weight
:
600
;
}
.product-table
tbody
tr
:hover
{
background-color
:
#f1f1f1
;
}
.product-table
input
{
width
:
60px
;
text-align
:
center
;
padding
:
5px
;
border
:
1px
solid
#ccc
;
border-radius
:
4px
;
}
.empty-cart
{
text-align
:
center
;
font-size
:
1.2em
;
color
:
#999
;
margin-bottom
:
25px
;
}
.total
{
text-align
:
right
;
font-size
:
1.6em
;
font-weight
:
bold
;
padding
:
15px
;
background-color
:
#f7f7f7
;
border-radius
:
5px
;
}
</
style
>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录