vue小案例(小黑记事本和购物车)

简介: vue小案例(小黑记事本和购物车)

vue小案例


1. 小黑记事本小案例


    <footer class="footer" >
      <span class="todo-count" v-if="list.length!=0">
        <strong>{{list.length}}</strong> items left
      </span>
        <button class="clear-completed" @click="clear" v-if="list.length!=0">
            Clear
        </button>
    </footer>
</section>
<!-- 底部 -->
<footer class="info">
    <p>
        <a href="http://www.itheima.com/"><img src="../image/black.png" alt="" /></a>
    </p>
</footer>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    var app = new Vue({
        el: "#todoapp",
        data: {
            list: ["写代码", "吃饭饭", "睡觉觉"],
            inputValue: "好好学习,天天向上"
        },
        methods: {
            add: function () {
                this.list.push(this.inputValue);
            },
            remove:function(index){
                console.log("删除");
                console.log(index);
                this.list.splice(index,1);
            },
            clear:function (){
                this.list = []
            }
        },
    })
</script>
</body>
</html>

2. 购物车小案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue实现购物车案例</title>
</head>
<body>
<div id="app">
    <div v-if="items.length">
        <h1> {{msg}}  computed{{  counts }}</h1>
        编号: <input type="text" v-model="item.id">
        名称: <input type="text" v-model="item.name">
        单价: <input type="text" v-model="item.price">
        数量: <input type="text" v-model="item.count">
        <button @click="addCount"> 添加到购物车 </button>
        <br><br><br>
        <table border="1">
            <tr>
                <th>编号</th>
                <th>名称</th>
                <th>单价</th>
                <th>数量</th>
                <th>小计</th>
                <th>操作</th>
            </tr>
            <tr v-for="(item,index) in items" :key="item.id">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.price}}</td>
                <td><input type="button" value="-" @click="remove(index)"> {{item.count}} <input type="button" value="+" @click="add(index)"> </td>
                <td>{{item.count * item.price}}</td>
                <td><button @click="removeClick(index)">删除</button></td>
            </tr>
        </table>
        <h3>总价格: {{ totalPrice }}</h3>
        <h3>总价格: {{ totalPrice }}</h3>
        <h3>总价格: {{ totalPrice }}</h3>
    </div>
    <h1 v-else>购物车为空</h1>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    var app = new Vue({
        el:'#app',
        data : {
            count:0,
            msg:'购物车案例',
            item : {},
            items:[
                {id:1,name:'iphone12',price:4899,count:1,delete:"删除"},
                {id:2,name:'redmik30pro',price:3199,count:1,delete: "删除"},
            ]
        },
        methods: {
            add:function (idx){
                this.items[idx].count++
            },
            remove:function (idx){
                if (this.items[idx].count>1) {
                    this.items[idx].count--
                } else {
                    alert('购买的商品不能少于一件')
                }
            },
            // totalPrice:function (){
            //     var totalprice = 0
            //     for (var i=0 ; i<this.items.length ; i++){
            //         totalprice +=this.items[i].price *this.items[i].count
            //     }
            //     return totalprice
            // },
            addCount:function (){
                if (!this.item.id){
                    alert('请输入编号');
                    return false
                }
                if (!this.item.name){
                    alert('请输入名称');
                    return false
                }
                if (!this.item.price){
                    alert('请输入价格');
                    return false
                }
                if (!this.item.count){
                    alert('请输入数量');
                    return false
                }
                if (!(this.item.count>0)){
                    alert('请输入正确数量');
                    return false
                }
                this.items.push(this.item)  //放入数组
            },
            removeClick(index){
                this.items.splice(index,1)
            }
        },
        computed : {
            counts:function (){
                return this.count+10
            },
            totalPrice:function (){
                // var totalprice = 0
                // // for (var i=0 ; i<this.items.length ; i++){
                // //     totalprice +=this.items[i].price *this.items[i].count
                // // }
                // for (let item of this.items) {
                //     totalprice +=item.price*item.count
                // }
                // return totalprice
                return this.items.reduce(function (pre,item){
                    return pre + item.price*item.count
                },0)
            },
        }
    })
</script>
</body>
</html>
相关文章
|
4月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
372 2
|
3月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
341 137
|
7月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
860 0
|
6月前
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
500 1
|
6月前
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
320 0
|
JavaScript 测试技术 容器
Vue2+VueRouter2+webpack 构建项目
1). 安装Node环境和npm包管理工具 检测版本 node -v npm -v 图1.png 2). 安装vue-cli(vue脚手架) npm install -g vue-cli --registry=https://registry.
1255 0
|
9月前
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
1111 4
|
7月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
8月前
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
1017 77
|
9月前
|
缓存 JavaScript 前端开发
Vue 基础语法介绍
Vue 基础语法介绍