vue script setup语法糖

创建项目

yarn 安装的 vue3.0+vite+ts

yarn create @vitejs/app my-vue-ts-vite --template vue-ts

示例

这个时候你在App.vue发现这个 script setup语法糖

<template>
  <h1><a href="">{{ count }}</a></h1>
  <h1>{{state.data.a}}</h1>
  <button @click="count++">count ++</button>
</template>

<script setup>
import {ref,defineProps,reactive,watch } from 'vue';
import {helloLog} from '/@/hooks/hello'
/*
基本数据类型
引用数据类型(复杂类型) 个人建议 ref初始化变量 
ref 和 reactive 本质我们可以简单的理解为ref是对reactive的二次包装, 
ref定义的数据访问的时候要多一个.value
*/
 const count =ref(0);
 const state = reactive({
   data:{a:1}
 })
 defineProps({
  msg:  {
      type: String,
      required: true
    }
})
watch(count,(e)=>{
console.log('count变化了:>> ', e);
})
function name(params) {
  console.log('原始html用法 ',helloLog());
}
// 
</script>

<style  scoped lang="scss" >
h1{
  a {
  color: #42b983;
}
}

label {
  margin: 0 0.5em;
  font-weight: bold;
}

code {
  background-color: #eee;
  padding: 2px 4px;
  border-radius: 4px;
  color: #304455;
}
</style>


总结模板

组件

<template>
  <div></div>
</template>

<script setup>
import {ref,defineProps,reactive,getCurrentInstance } from 'vue';
    const internalInstance = getCurrentInstance();//获取当前实例
    const route = internalInstance?.appContext.config.globalProperties.$route;

/*
基本数据类型
引用数据类型(复杂类型) 个人建议 ref初始化变量 
ref 和 reactive 本质我们可以简单的理解为ref是对reactive的二次包装, 
ref定义的数据访问的时候要多一个.value
*/
 const count =ref(0);
 const state = reactive({
   data:{a:1}
 })

 defineProps({
  msg:  {
      type: String,
      required: true
    }
})

</script>

<style  scoped lang="scss" >

</style>

普通页面

<template>
  <div>
      <HelloWorld/>
  </div>
</template>

<script setup>
import {ref,defineProps,reactive,getCurrentInstance } from 'vue';
    const internalInstance = getCurrentInstance();//获取当前实例
    const route = internalInstance?.appContext.config.globalProperties.$route;
import HelloWorld from '/@/components/HelloWorld.vue'
/*
基本数据类型
引用数据类型(复杂类型) 个人建议 ref初始化变量 
ref 和 reactive 本质我们可以简单的理解为ref是对reactive的二次包装, 
ref定义的数据访问的时候要多一个.value
*/
 const count =ref(0);
 const state = reactive({
   data:{a:1}
 })


</script>

<style  scoped lang="scss" >

</style>

个人评价

script setup语法糖提供了传统html函数使用方式的便利性+组件化开发的效率,组件导入即可使用无需扔到components,需要计算属性引入 watch即可

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值