用vue做一个简单的button

项目准备

安装parcel

npm i -D parcel-bundler

-D的意思是给开发人员用的.

基本结构

index.html
src
    button.vue
    app.js 

在index.html中引入src/app.js, 而app.js中引入了vue和button.vue.

src下的app.js和button.vue

在src中写下button.vue, 这是一个单页应用

<template>
    <button class="g-button">按钮</button>
</template>
<script>
    export default {}
</script>
<style lang="scss">
/*var后面的是变量, 在index.html中声明*/
    .g-button {
        font-size: var(--font-size);
        height: var(--button-height);
        padding: 0 1em;
        border-radius: var(--border-radius);
        border: 1px solid var(--border-color);
        background: var(--button-bg);
        &:hover {
            border-color: var(--border-color-hover);
        }
        &:active {
            background-color: var(--button-active-bg);
        }
        &:focus{
            outline: none;
        }
    }
</style>

vue的单页应用有三个部分, <template>, <script>和<style>.
注意style中我们用的时sass语法.

在app.js中我们需要引入vue和button.vue. 并将button组件命名为g-button, 这样就可以在index.html中世界使用g-button了(当然index.html需要引入)

import Vue from 'vue'
import Button from './button'

Vue.component('g-button', Button)

var app = new Vue({
    el: '#app'
})

index.html

最后在index.html中引入app.js

<div id="app">
    <g-button></g-button>
</div>
<script src="./src/app.js"></script>

不要忘了给其样式, 这个样式有button.vue中要用到的变量

:root{
    --font-size: 14px;
    --button-height: 32px;
    --button-bg: white;
    --button-active-bg: #eee;
    --border-radius: 4px;
    --color: #333;
    --border-color: #999;
    --border-color-hover: #666;
}
* {margin: 0; padding: 0; box-sizing: border-box;}
#app {
    margin: 20px;
}
body {
    font-size: var(--font-size);
}

执行

我们已经安装了parcel, 那么我们就可以了使用她来构建我们的应用.

./node_modules/.bin/parcel --no--cache index.html 

执行上面的代码, 按照提示打开浏览器, 我们就可以了看到按钮了.

--no-cache是为了不使用缓存.
./node_modules/.bin/parcel 也可以是引用npx parcel来代替.

最后的执行结果如图:
这里写图片描述

源代码可以在这里查看:
https://github.com/helloyongwei/vue-example-wheels

Vue.js中创建一个简单的计算器,你可以遵循以下几个步骤: 1. **安装 Vue**:首先确保已安装Node.js,然后全局安装Vue CLI: ``` npm install -g @vue/cli ``` 2. **创建项目**: ```bash vue create calculator-app cd calculator-app ``` 3. **构建组件**: a. 在`src/components`目录下创建一个新的文件夹叫`Calculator`, 然后在其中创建一个名为`Calculator.vue`的文件。在这个文件里,创建一个包含按钮、显示框和操作符的结构,如下面这个例子: ```html <template> <div> <input v-model.number="result" type="number" disabled :value="displayValue"> <div v-for="(operator, index) in operators" :key="index"> <button @click="applyOperator(operator)" :class="{ active: selectedOperator === operator }">{{ operator }}</button> </div> <button @click="clearResult">清除</button> <button @click="calculate">计算</button> </div> </template> <script> export default { data() { return { result: '', displayValue: '0', selectedOperator: '', operators: ['+', '-', '*', '/'], }; }, methods: { applyOperator(operator) { this.selectedOperator = operator; }, calculate() { const num1 = parseFloat(this.displayValue); const num2 = parseFloat(this.result); let result; switch (this.selectedOperator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': if (num2 !== 0) { result = num1 / num2; } else { alert('除数不能为零'); } break; default: // 如果没有选择任何运算符,结果不变 break; } this.result = result; this.selectedOperator = ''; this.clearResult(); }, clearResult() { this.result = ''; this.displayValue = '0'; }, }, }; </script> ``` 4. **主入口文件(main.js)**中引入并使用这个组件,并在App.vue或者其他适当的地方挂载它。 5. **运行应用**: ```bash npm run serve ``` 现在你已经创建了一个基本的Vue计算器,用户可以输入数字,选择运算符,点击相应按钮,然后进行计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值