vue3.0的多种写法,你喜欢哪种呢?

第一种写法:
<template>
  <div>defineComponent</div>
  <div>{{ topinset }}</div>
  <div>{{ doubleCount }}</div>
  <div>{{ list }}</div>
</template>

<script lang="ts">
interface STATE {
  topinset: number;
}
import {
  defineComponent,
  reactive,
  toRefs,
  onMounted,
  nextTick,
  computed,
  watch
} from "vue";

export default defineComponent({
  name: "App",
  props: ["list"],
  emits: ["openAgain"],
  components: {},
  setup(prop, context) {
    // emit
    // context.emit("openAgain");
    // prop
    // prop.list
    const state = reactive<STATE>({
      topinset: 0
    });
    const doubleCount = computed(() => state.topinset + "1");
    onMounted(() => {
      console.log("onMounted");
    });
    nextTick(() => {
      console.log("nextTick");
    });
    setTimeout(() => {
      state.topinset = 3;
    }, 1000);
    watch(
      () => state.topinset,
      (newValue: any) => {
        console.log("watch");
      }
    );
    return {
      doubleCount,
      ...toRefs(state)
    };
  }
});
</script>
第二种写法
<template>
  <div>
    classComponent
    <div>{{ balance }} {{ balance1 }}</div>
    <img src="@/assets/bg.png" alt="" />
  </div>
</template>

<script lang="ts">
import { Vue, Options, prop } from "vue-class-component";
class Props {
  public list = prop<number>({ default: 0 });
}
@Options({
  components: {},
  watch: {
    balance: function (val) {
      console.log("watch");
    }
  }
})
export default class Cla extends Vue.with(Props) {
  balance: string = "0.00";

  created() {
    console.log("created");
	
	//this.$emit("close");
	
    setTimeout(() => {
      this.balance = "123";
    }, 1000);
	
    this.$nextTick(() => {
      console.log("nextTick");
    });
  }

  get balance1() {
    return this.balance + "2342";
  }

  mounted() {
    console.log("mounted");
  }
}
</script>

第三种写法

vue3.2之后的版本

<template>
  <div>setup</div>
  <div>{{ a }}</div>
  <div>{{ ab }}</div>
  <HelloWorld :a="ab" :b="a" />
</template>

<script lang="ts" setup>
import { ref, nextTick, computed, watch } from "vue";
// 组件直接import
import HelloWorld from "@/components/HelloWorld.vue";
// defineProps<{ a: string; b: string }>();
// defineEmits
let a = ref("小明");
const ab = computed(() => a.value + "1");
nextTick(() => {
  console.log("nextTick");
});
setTimeout(() => {
  a.value = "kkkk";
}, 1000);
watch(
  () => a.value,
  (newValue: any) => {
    console.log("watch", newValue);
  }
);
</script>

项目地址

https://github.com/jwnoal/vite_vue3

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值