count有很多种变换方式

0刷新一下变成1;
0自动变成1,2,3,……
0点加号按钮,变成50,100,150,周五才做过的
0点过两秒变成1;

刷新一下变成1,核心代码

const count = ref(0)
count.value = 1

即可

0点加号变成50
完全不会,又忘得一干二净

<template>
  <p>{{ count }}</p>
  <button>++++</button>
</template>

<script>
import { reactive, ref } from "vue";
  export default {
    name: "App",
    setup() {
      const count = ref(0)
      

      return {
        count,
      };
    },
  };

  
  </script>
  <style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>


不会
想了一下,只能写成这样了
App.vue

<template>
  <test :count="add"></test>
  
</template>

<script>
import { reactive, ref } from "vue";
import Test from "./components/Test.vue"
  export default {
    name: "App",
    components: {
      Test,
    },
    setup() {
      const count = ref(0)
      const add = Num { num = count += count}

      return {
        count,
      };
    },
  };

  
  </script>
  <style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>


Test.vue
不记得了

App.vue写好了,但响应式没写好,Test的?再去改改看。

<template>
  <test :count="add"></test>
  
</template>

<script>
import { reactive, ref } from "vue";
import Test from "./components/Test.vue"
  export default {
    name: "App",
    components: {
      Test,
    },
    setup() {
      const count = ref(0)
      const add =  (num) => ( count.value += num );

      return {
        count,
        add,
      };
    },
  };

Test错误的

<template>
  <div>{{ count }}</div>
  <button @click="add">++++</button>
</template>

<script>
//import { watchEffect } from 'vue'
export default {
  name: "Test",
  props: {
    count: Number
  },
  setup(props) {
    console.log("props", props);
//    watchEffect(() => {
//    console.log('props.count = ', props.count)
  },
};

</script>

<template>
  <test :count="count" @add="add"></test>
  
</template>

<script>
import { ref } from "vue";
import Test from "./components/Test.vue";
  export default {
    name: "App",
    components: {
      Test,
    },
    setup() {
      const count = ref(0);
      const add =  (num) => (count.value += num);

      return {
        count,
        add,
      };
    },
  };

  
  </script>
  <style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>


正确的,App.vue

<template>
  <test :count="count" @add="add"></test>
  
</template>

<script>
import { ref } from "vue";
import Test from "./components/Test.vue";
  export default {
    name: "App",
    components: {
      Test,
    },
    setup() {
      const count = ref(0);
      const add =  (num) => (count.value += num);

      return {
        count,
        add,
      };
    },
  };

  
  </script>
  <style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>


Test.vue

<template>
  <div>{{ count }}</div>
  <button @click="add">++++</button>
</template>

<script>
import { watchEffect } from 'vue'
export default {
  name: "Test",
  props: {
    count: Number,
    add: Function,
  },
  emits: ["add"], //为什么要跟这个,不跟会怎么样?
  setup(props,ctx) {
    const add = () => {
      ctx.emit("add", 50);
    }
    return {
      add,
    };
  },
};
//    console.log("props", props);
//    watchEffect(() => {
//    console.log('props.count = ', props.count)
 

</script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值