了解script setup语法糖

2023-05-16

了解<script setup>语法糖

基本语法

<script setup lang="ts">
  console.log('hello script setup');
</script>

里面的代码会被编译成组件 setup() 函数的内容。

传统vue2

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

<script>
export default {
name: "index",
mounted() {
 console.log('hello script setup');
},
};
</script>

顶层的绑定会被暴露给模板

<script setup lang="ts">
// 变量
const msg = 'Hello!'

// 函数
function log() {
  console.log(msg)
}
</script>

<template>
  <div @click="log">{{ msg }}</div>
</template>

当使用 <script setup> 的时候,任何在 <script setup> 声明的顶层的绑定 (包括变量,函数声明,以及 import 引入的内容) 都能在模板中直接使用;

例如msg and log() 默认暴露出去,在模版template中可直接使用

传统vue2

<template>
<div>
 <h2 @click="log">{{msg}}</h2>
</div>
</template>

<script>
export default {
name: "index",
data() {
 return {
   msg: 'Hello!'
 };
},
methods: {
 log() {
   console.log(this.msg)
 }
}
};
</script>

<style scoped></style>

使用组件

<script setup lang="ts">
import MyComponent from './MyComponent.vue'
</script>

<template>
  <MyComponent />
</template>

MyComponent 看做被一个变量所引用。

传统vue2

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

<script>
import table3 from "@/components/table3";
export default {
name: "index",
components: {
 table3
}
};
</script>

<style scoped></style>

defineProps

App.vue(父组件)
<script setup lang="ts">
// 这个启动器模板使用Vue 3
// 请查看https://vuejs.org/api/sfc-script-setup.html script-setup

import HelloWorld from "./components/HelloWorld.vue";
</script>

<template>
  <HelloWorld msg="Hello Vue 3 + TypeScript + Vite" />
</template>

<style>
</style>
HelloWorld.vue(子组件)
<script setup lang="ts">
interface Props {
  msg?: string;
  value?: string;
}

withDefaults(defineProps<Props>(), {
  msg: "hello",
});
</script>

<template>
  <h1>{{ msg }}</h1>
</template>

<style scoped>
</style>

使用类型声明时的默认 props 值

仅限类型的 defineProps 声明的不足之处在于,它没有可以给 props 提供默认值的方式。为了解决这个问题,提供了 withDefaults 编译器宏

defineEmits

App.vue(父组件)
<script setup lang="ts">
// 这个启动器模板使用Vue 3
// 请查看https://vuejs.org/api/sfc-script-setup.html script-setup

import HelloWorld from "./components/HelloWorld.vue";
  
const Hello = () => {
  console.log("Hello");
};
  
const Hello2 = () => {
  console.log("Hello2");
};
</script>

<template>
  <HelloWorld @change="Hello" @update="Hello2" />
</template>

<style>
</style>
HelloWorld.vue(子组件)
<script setup lang="ts">
const emit = defineEmits<{
  (e: "change", id: number): void;
  (e: "update", value: string): void;
}>();
  
const decrement = () => emit('update', '456');
</script>

<template>
  <button type="button" @click="$emit('change', 123)">btn1</button>

  <button type="button" @click="decrement">btn2</button>
</template>

<style scoped>
</style>

演示了两种调用方式

slot

App.vue(父组件)
<script setup lang="ts">
import HelloWorld from "./components/HelloWorld.vue";
</script>

<template>
  <HelloWorld>
    <h3>实现插槽1</h3>
    <template v-slot:dome>
      <h4>实现插槽2</h4>
    </template>
  </HelloWorld>
</template>

<style>
</style>

使用插槽时,不能使用 slot="XXX",要使用v-slot,不然会报错

HelloWorld.vue(子组件)
<script setup lang="ts">
</script>

<template>
  <slot></slot>
  <slot name="dome"></slot>
</template>

<style scoped>
</style>

顶层 await

<script setup> 中可以使用顶层 await。结果代码会被编译成 async setup():

<script setup>
const post = await fetch(`/api/post/1`).then(r => r.json())
</script>

限制:没有 Src 导入

由于模块执行语义的差异,<script setup> 中的代码依赖单文件组件的上下文。当将其移动到外部的 .js 或者 .ts 文件中的时候,对于开发者和工具来说都会感到混乱。因而 <script setup> 不能和 src attribute 一起使用。

其它的感觉暂时用不到,所以暂时不写,更多细节查看 => 🌈 官方文档

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

了解script setup语法糖 的相关文章

随机推荐

  • C/C++中如何遍历枚举类型?

    在C 43 43 中 xff0c 写一段代码 xff0c 把某个enum中的所有元素放入到vector中 xff0c 方便遍历 假设有以下的枚举类型 xff1a span class token comment c 43 43 span s
  • 201609-3 炉石传说

    201609 3 炉石传说 题意描述 本题要求我们去模拟一款游戏的运行 xff0c 游戏分为双方 轮流操作 操作共有三种形式 summon attack end 输入输出格式 输入 n 接下来是 n 个操作 每个操作由字符串区分 后面是操作
  • 超详细Windows10/Windows11 子系统(WSL2)安装Ubuntu20.04(带桌面环境)+CUDA11.3+pytorch1.8.1+pycharm

    超详细Windows10 Windows11 子系统 xff08 WSL2 xff09 安装Ubuntu20 04 xff08 带桌面环境 xff09 user zhaowei的博客 CSDN博客 wsl 桌面环境 目录前言在Windows
  • 洛谷_P1825 [USACO11OPEN]Corn Maze S(尚贤)

    题目 xff1a https www luogu com cn problem P1825 span class token macro property span class token directive keyword include
  • 从知名软件提取出的神器,吊打一众付费

    前言 现在的软件功能虽然越更新越多 xff0c 但也是越来越臃肿了 xff0c 不仅占内存 启动慢 xff0c 老更新也怪麻烦的 你们应该也有过因为某个小功能挺好用而不忍心卸载整款软件的情况吧 今天给大家分享几款从知名软件中提取出来的小工具
  • Ubuntu 设置网络代理的一些配置

    因公司需求 xff0c 连接网络必须使用代理设置连接网络 xff0c 这里给大家分享一下我的配置步骤 xff01 如果以下这些不符合你的需求 xff1a 来看这个 xff1a ubuntu 内网搭建服务器的一些配置 xff0c 完美解决ht
  • Linux上git+gitee的基本使用

    目录 1 git 2 gitee 3 本地仓库建立和git基本使用 1 远程仓库同步到本地仓库 xff1a 2 提交本地仓库到远程仓库进行更新 如在本地代码仓库中新建了test1 txt和test2 txt文件或者对文件内容进行了修改 xf
  • java编程题练习2

    程序6 题目 xff1a 输入两个正整数m和n xff0c 求其最大公约数和最小公倍数 在循环中 xff0c 只要除数不等于0 xff0c 用较大数除以较小的数 xff0c 将小的一个数作为下一轮循环的大数 xff0c 取得的余数作为下一轮
  • 安装Anaconda后,cmd输入python没有结果

    可能是电脑上已经安装过python xff0c 在cmd中输入 where python 查看电脑上所有python解释器所在的路径 路径优先级最高的解释器 xff08 非Anaconda所在路径 xff09 出现损坏 xff0c 或由于其
  • 用python发送163邮件

    邮件是最早有用计算机通信的方式之一 xff0c 采用pop3协议接受邮件 xff0c smtp协议发送邮件 xff0c 基本上所有的计算机通信建立在tcp udp协议之上 xff0c 邮件传输协议也是不列外的 如果想要用编程语言发送邮件 x
  • 【AWS EC2】云端简单部署NodeJS应用

    根据需要启动想要的EC2 注意 xff1a 选择Linux时用x86而不是arm xff0c 不然kafka和zookeeper启动不了 xff08 在我的项目里 xff09 并且bitnami zookeeper latest需要内存超过
  • 在kali linux上安装git与push提交

    在kali linux上安装git与push提交 写在前面 xff1a 最好不要在root账户下创建 xff0c 在使用vscode时会带来好多问题 1 安装git apt get install git 2 添加git服务用户 邮箱 xf
  • 判断一颗二叉树是否为完全二叉树

    package main import 34 container list 34 思路 层序遍历 只需分两种情况 1 xff09 若当前节点左孩子为空右孩子不为空 直接返回不空 2 xff09 若当前节点的左右孩子不全 则其后面的节点必须都
  • 【mybatis-plus】mybatis-plus代码生成器,自动生成controller、service、dao、mapper、pojo代码,可灵活配置生成路径,程序猿的福音!!!

    在我们需要一些数据库的实体类时 xff0c 需要手动创建实体与类 xff0c 这很浪费时间 xff0c 所以我研究了一下和上网找了一些资料 xff0c 整合出了一套可灵活配置生成路径的代码 xff01 xff01 xff01 直接上代码 s
  • python爬虫:爬取携程航班数据

    python爬虫 xff1a 爬取携程航班数据 最近在学爬虫 xff0c 用携程的航班数据练手 xff0c 顺便记录一下 xff0c 话不多说下面开始 xff1a 一 首先来观察一下携程网的航班信息的网页 xff1a 这是一个携程网站的截图
  • PyQt5入门和常用模块(含多线程简单例子)

    1 安装软件包 pip span class token function install span PyQt5 pip span class token function install span pyqt5 tools 2 安装Qtde
  • CentOS 8.5高性能计算开发环境配置备忘

    CentOS 8 5环境配置 在CentOS 8 5系统下配置高性能计算环境 主要包含编译器套件 分布式并行套件 各种数值计算库 Python环境 辅助管理工具 GPU开发环境等 利用CentOS自带的RPM仓库 xff0c 加上OpenH
  • Python3 + BeautifulSoup 爬取Steam热销商品数据

    这次用了BeautifulSoup库来爬取Steam的热销商品 xff0c BeautifulSoup更侧重的是从页面的结构解析 xff0c 根据标签元素等来爬取数据 xff0c 这次遇到两个问题 xff1a 1 Steam热销商品列表经常
  • C语言中用%输出不同内容

    C语言有很多输出函数 xff0c 也有很多输出方式 xff0c 但最基础的还是printf 函数 xff0c 但我们不止能输出想要的内容还能指定输出的方式 xff0c 下面我就对此总结一下 xff0c 本人也是初学者 xff0c 以下内容可
  • 了解script setup语法糖

    了解 lt script setup gt 语法糖 基本语法 lt script setup lang 61 34 ts 34 gt console log 39 hello script setup 39 lt script gt 里面的