笔记

安装 vue-resource 包

npm i vue-resource

引入 vue-resource 包,会在 vc 上多一个 $http ,如下图所示:

106_求和案例_纯vue版 - 图1

重要文件源码

components\count.vue

<template>
    <div>
        <h1>当前求和为:{{sum}}</h1>

        <!-- 步进 
        .number 修饰符的作用: 将收集到的内容,强制转换为 数值。 
        -->
        <select v-model.number="n">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>

        <button @click="increment">+</button>
        <button @click="decrement">-</button>

        <button @click="incrementOdd">当前 求和为 奇数 时,才允许再加</button>
        <button @click="incrementWait">相邻两次需要延时500ms, 才允许再加</button>
    </div>
</template>

<script>
    export default {
        name:'Count',
        data() {
            return {
                n:1, //用户选择的数字
                sum:0 //当前的和
            }
        },
        methods: {
            increment(){
                this.sum += this.n
            },
            decrement(){
                this.sum -= this.n
            },
            incrementOdd(){
                if(this.sum % 2){
                    this.sum += this.n
                }
            },
            incrementWait(){
                setTimeout(()=>{
                    this.sum += this.n
                },500)
            },
        },
    }
</script>

<style lang="css">
    button{
        margin-left: 5px;
    }
</style>