基于Vue前端框架的一个简单的网页计算器

还在学习阶段,代码很粗糙,请大佬们指正,也希望能给其他正在学习Vue的人一点启发

 Vue的代码只用到了v-model和v-on

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算器plus</title>
    <script src="js/vue.min.js"></script>
</head>
<body>
<div id="app">
    <input style="height: 50px;width: 220px" v-model="result" type="text"><br>
    <input type="button" @click="flag(7)" value="7" style="height: 50px ;width: 50px">&nbsp;
    <input type="button" @click="flag(8)" value="8" style="height: 50px ;width: 50px">&nbsp;
    <input type="button" @click="flag(9)" value="9" style="height: 50px ;width: 50px">&nbsp;
    <input type="button" @click="clear()" value="C" style="height: 50px ;width: 50px">&nbsp;
    <br>
    <input type="button" @click="flag(4)" value="4" style="height: 50px ;width: 50px">&nbsp;
    <input type="button" @click="flag(5)" value="5" style="height: 50px ;width: 50px">&nbsp;
    <input type="button" @click="flag(6)" value="6" style="height: 50px ;width: 50px">&nbsp;
    <input type="button" @click="flag('-')" value="-" style="height: 50px ;width: 50px">&nbsp;
    <br>
    <input type="button" @click="flag(1)" value="1" style="height: 50px ;width: 50px">&nbsp;
    <input type="button" @click="flag(2)" value="2" style="height: 50px ;width: 50px">&nbsp;
    <input type="button" @click="flag(3)" value="3" style="height: 50px ;width: 50px">&nbsp;
    <input type="button" @click="flag('+')" value="+" style="height: 50px ;width: 50px">&nbsp;
    <br>
    <input type="button" @click="flag('/')" value="÷" style="height: 50px ;width: 50px">&nbsp;
    <input type="button" @click="flag(0)" value="0" style="height: 50px ;width: 50px">&nbsp;
    <input type="button" @click="flag('*')" value="*" style="height: 50px ;width: 50px">&nbsp;
    <input type="button" @click="count()" value="=" style="height: 50px ;width: 50px">&nbsp;
    <br>
    <input type="text" v-model="firstNum" ><br>
    <input type="text" v-model="symbol" ><br>
    <input type="text" v-model="lastNum" ><br>
</div>
</body>
<script>
    new Vue({
        el: '#app',
        data: {
            firstNum: "",
            symbol: "",
            lastNum: "",
            result: ""
        },
        methods: {
            flag(e) {
                if (this.symbol == "") {
                    if (e != "+" && e != "-" && e != "*" && e != "/" && e != "C") {
                        this.firstNum += e
                        this.result += e
                    } else {
                        this.symbol = e
                        this.result += e
                    }
                } else if (e != "+" && e != "-" && e != "*" && e != "/" && e != "C") {
                    this.lastNum += e
                    this.result += e
                } else {
                    switch (this.symbol) {
                        case '+':
                            this.result = Number(this.firstNum) + Number(this.lastNum)
                            this.firstNum = this.result
                            this.symbol = e
                            this.lastNum = ""
                            break
                        case '-':
                            this.result = Number(this.firstNum) - Number(this.lastNum)
                            this.firstNum = this.result
                            this.symbol = e
                            this.lastNum = ""
                            break
                        case '*':
                            this.result = Number(this.firstNum) * Number(this.lastNum)
                            this.firstNum = this.result
                            this.symbol = e
                            this.lastNum = ""
                            break
                        case '/':
                            this.result = Number(this.firstNum) / Number(this.lastNum)
                            this.firstNum = this.result
                            this.symbol = e
                            this.lastNum = ""
                            break
                    }
                    this.result += this.symbol
                }
            },
            count() {
                switch (this.symbol) {
                    case '+':
                        this.result = Number(this.firstNum) + Number(this.lastNum)
                        this.firstNum = this.result
                        this.symbol = ""
                        this.lastNum = ""
                        break
                    case '-':
                        this.result = Number(this.firstNum) - Number(this.lastNum)
                        this.firstNum = this.result
                        this.symbol = ""
                        this.lastNum = ""
                        break
                    case '*':
                        this.result = Number(this.firstNum) * Number(this.lastNum)
                        this.firstNum = this.result
                        this.symbol = ""
                        this.lastNum = ""
                        break
                    case '/':
                        this.result = Number(this.firstNum) / Number(this.lastNum)
                        this.firstNum = this.result
                        this.symbol = ""
                        this.lastNum = ""
                        break
                }
            },
            clear() {
                this.firstNum = ""
                this.symbol = ""
                this.lastNum = ""
                this.result = ""
            }
        }
    })
</script>
</html>

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个基于Vue框架实现的简单网页项目的步骤: 1. 安装Vue CLI Vue CLI是一个官方提供的脚手架工具,可以快速创建一个Vue项目。在命令行中运行以下命令进行安装: ``` npm install -g @vue/cli ``` 2. 创建一个Vue项目 使用Vue CLI创建一个新项目,运行以下命令: ``` vue create my-project ``` 其中,my-project是你的项目名称。在创建项目的过程中,你可以选择要安装哪些特性和插件。 3. 编写页面组件 在src/components目录下,创建一个名为MyPage.vue的文件,用于编写页面组件。在这个组件中,你可以使用Vue的模板语法、数据绑定、计算属性等功能来编写页面布局和渲染逻辑。 4. 编写路由配置 在src/router目录下,创建一个名为index.js的文件,用于配置页面路由。在这个文件中,你可以使用Vue Router来定义路由规则,并将页面组件与路由关联起来。 5. 编写数据管理逻辑 在src/store目录下,创建一个名为index.js的文件,用于编写应用的数据管理逻辑。在这个文件中,你可以使用Vuex来管理应用的状态、数据、行为等。 6. 运行应用 在命令行中运行以下命令,启动开发服务器: ``` npm run serve ``` 然后在浏览器中访问http://localhost:8080,就可以看到你的网页应用了。 以上就是基于Vue框架实现一个网页项目的简单步骤。当然,具体的实现过程和代码细节还需要根据你的具体需求来进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值