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即可

### Vue3 中使用 Setup 语法糖实现点击事件为 `reactive` 对象新增属性并使用 `toRefs` 在 Vue3 的组合式 API 中,`setup` 函数提供了一种更灵活的方式来处理组件的状态逻辑。通过 `reactive` 创建的对象可以动态添加新属性,并且可以通过 `toRefs` 将其解构为独立的响应式引用。 以下是一个完整的示例,展示如何在点击事件中为 `reactive` 对象新增一个属性,并利用 `toRefs` 进行解构和绑定: #### 示例代码 ```vue <template> <div> <!-- 绑定现有属性 --> <p>Name: {{ name }}</p> <p>Age: {{ age }}</p> <!-- 显示新增属性 --> <p v-if="isNewPropAdded">New Property: {{ newProperty }}</p> <!-- 添加新属性的按钮 --> <button @click="addNewProp">Add New Property</button> </div> </template> <script setup> import { reactive, toRefs } from 'vue'; // 创建一个初始的 reactive 对象 const state = reactive({ name: 'John Doe', age: 30, }); // 是否已添加新属性的标志 let isNewPropAdded = false; // 方法:动态添加新属性 function addNewProp() { if (!('newProperty' in state)) { state.newProperty = 'This is a dynamically added property'; isNewPropAdded = true; } } // 使用 toRefs 解构 reactive 对象的属性 const { name, age, newProperty } = toRefs(state); // 导出给模板使用 defineExpose({ addNewProp }); </script> ``` --- #### 关键点解析 1. **动态添加属性** 在 `addNewProp` 方法中,检查目标对象是否已经存在名为 `newProperty` 的属性。如果没有,则通过直接赋值的方式为其添加新属性。由于 `state` 是由 `reactive` 创建的,因此该属性会自动成为响应式的[^1]。 2. **使用 `toRefs` 解构** 调用 `toRefs` 后,`state` 对象中的每个属性都会被转换成单独的 `ref`。这样做的好处是可以将这些属性安全地传递到子组件或其他上下文中,同时保持它们的响应性[^2]。 3. **条件渲染新增属性** 利用了布尔变量 `isNewPropAdded` 来控制新属性是否显示。只有当新属性成功添加后,才会触发对应的 DOM 更新[^3]。 4. **Setup 语法糖的优势** 借助 `<script setup>` 语法糖,省去了显式返回 `return` 的过程,使得代码更加简洁明了。所有声明的内容默认都可以在模板中直接访问[^4]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值