Vue3 setup语法糖

2022-06-27  本文已影响0人  宏势

Vue3 支持三种组件写法:
1. defineComponent+组合式API 2.<script setup>语法糖 3.选项式写法与vue2写法一样
前面两种是完全吻合typescript写法,推荐使用第二种方法,简洁清晰;第三种是javascript的写法,vue3已经全面支持typescript了,不建议使用了

一、defineComponent+组合式API

<template>
  <div>
    <h1 :style="{color:color}">{{title}}:{{count}}</h1>
    <a-button @click="btnClick" type="primary">点击</a-button>
  </div>
</template>

<script>

import {computed, defineComponent, ref, toRefs} from "vue";

export default defineComponent({
  name:"DefineComponent",
  components:{},
  props:{
    title:{
      type:String,
      required:true
    },
    init: {
      type: Number,
      default: 10
    }
  },
  emits: [],
  setup(props, context) {
    const count = ref(props.init)
    const btnClick = function (){
      count.value++;
    }
    const color = computed(()=> (count.value > 12) ? "red": "black")
    return {
      count,
      btnClick,
      color,
    }
  }
})

</script>

引用组件:

 <define-component title="defineComponent click count" :init="10"></define-component>

执行 setup 时,你只能访问以下 property: props attrs slots emit
将无法访问一下组件选项 data computed methods refs (模板 ref)

Vue3.x新增了以下核心特性:

1.组合式API+setup入口

组合式API主要是为提取可重用的代码段,提高应用的可维护性和灵活性,而setup 函数是组合式API的入口

2.ref、reactive、toRef、toRefs、unref 区别

const count = ref(0)
const state = reactive({
  foo: 1,
  bar: 2
})
count.value++
state.foo++
console.log(count.value) // 1
console.log(state.foo) // 2
const count = ref(0)
const state = reactive({
  foo: 1,
  bar: 2
})
console.log(unref(count)); // 输出0
const state1 = toRefs(state);
const showFoo = state1.foo; // showFoo是ref对象
showFoo.value++
console.log(state1); //输出state对象
const state = reactive({
  foo: 1,
  bar: 2
})
const fooRef = toRef(state, 'foo')

fooRef.value++
console.log(state.foo) // 2
state.foo++
console.log(fooRef.value) // 3

详细查看官网:https://v3.cn.vuejs.org/api/refs-api.html

二、<script setup>语法糖

<template>
  <div>
    <h1 :style="{color:color}">{{title}}:{{count}}</h1>
    <a-button @click="btnClick" type="primary">点击</a-button>
  </div>
</template>
<script lang="ts">
  export default {
    name:"SetupViewTest"
  }
</script>
<script setup lang="ts">
import {computed, ref, toRefs} from "vue";

  const props = defineProps({
    title:{
      type:String,
      required:true,
    },
    init:{
      type:Number,
      default:8,
    },
  });
  const emit = defineEmits([]);
  const count = ref(props.init);
  const btnClick = function (){
    count.value++;
  };
  const color = computed(()=> (count.value > 12) ? "red": "black");
</script>

引用组件:

 <setup-view-test title="setup click count" :init="8"></setup-view-test>

1.<script setup>语法糖其实是上面defineComponent方式的简写
2.无法对组件的name进行定义,可以采用以下两种方式定义name

<script lang="ts">
  export default {
    name:"SetupViewTest"
  }
</script>
npm install unplugin-vue-define-options --save-dev
defineOptions({
    name: 'SetupViewTest',
});

三、选项式写法 与vue2写法一样

<template>
  <div>
    <h1 :style="{color:color}">{{title}}:{{count}}</h1>
    <a-button @click="btnClick" type="primary">点击</a-button>
  </div>
</template>

<script>
export default {
  name:'Vue2Demo',
  props:{
    title:{
      type:String,
      required:true,
    },
    init:{
      type:Number,
      default:8
    }
  },
  data() {
    return{
      count: this.init,
    }
  },
  methods:{
      btnClick:function (){
        this.count++;
      }
  },
  computed:{
    color:function (){
        return  (this.count > 10) ? "red": "black";
    }
  }
}
</script>

引用组件:

<vue2-demo title="click count" :init="6"></vue2-demo>
上一篇 下一篇

猜你喜欢

热点阅读