手机网站怎么做301,关键词优化公司推荐,做简单网站用什么软件有哪些,长沙好博网站建设有限公司script setup 是在单文件中使用 Composition API 的编译时语法糖#xff0c;里面的代码会被编译成组件 setup() 函数的内容。 script setup 中的代码在每次组件实例被创建的时候都都会被执行。 定义数据#xff1a;
在 script setup 语法糖的写法中…script setup 是在单文件中使用 Composition API 的编译时语法糖里面的代码会被编译成组件 setup() 函数的内容。 script setup 中的代码在每次组件实例被创建的时候都都会被执行。 定义数据
在 script setup 语法糖的写法中顶层的绑定会暴露给模板因此在 script setup 中定义的变量、函数等可以直接使用。不需要像在 setup() 中一样 return 返回。
templatediv{{ message }}/div
/template!-- 在 script setup 中编写 Composition API 就相当于是在 setup() 函数中编写 Composition API --
script setup
import {ref} from vue// 在 script setup 顶层编写的代码都会被暴露给 template 模板因此在 script setup 中定义的变量、函数等不需要像在 setup() 中一样 return 返回可以直接使用
const message ref(Hello Vue)
/scriptstyle scoped
/style 导入组件
在 script setup 语法糖的写法中导入的组件可以直接使用。不需要像在 setup() 中一样手动通过 components 注册。
templateHome/Home
/templatescript setup
// 导入的组件不需要像在 setup() 中一样手动通过 components 注册可以直接使用
import {Home} from ./components/Home.vue
/scriptstyle scoped
/style 接收 props 属性、发出 emit 事件
在 script setup 语法糖的写法中通过 defineProps() 函数定义要接收的 props参数是一个对象定义接收的 props返回值是一个只读的 props 对象。 defineProps() 函数默认就在当前作用域中不需要导入。 templatediv{{ name }} - {{ age }}/div
/templatescript setup
// 通过 defineProps() 函数定义接收的 props
const props defineProps({name: {type: String,default: Lee,},age: {type: Number,default: 18,}
})
console.log(props)
/scriptstyle scoped
/style 在 script setup 语法糖的写法中通过 defineEmits() 定义要发出的事件返回值是一个函数调用返回的函数可以发出事件。 defineEmits() 函数默认就在当前作用域中不需要导入。 templatebutton cickhandleUserInfoChange修改/button/templatescript setup// 1. 通过 defineEmits() 定义要发出的事件const emits defineEmits([userInfoChange])const handleUserInfoChange () {// 2. 调用 defineEmits() 返回的函数发出手机哦啊吗emits(userInfoChange, 将名字改为 Tom)}/scriptstyle scoped/style 暴露数据
在 script setup 语法糖的写法中组件中的数据、方法等如果想要其他组件能够通过其组件实例获取到必须通过 defineExpose() 暴露出去。不能像在 setup() 函数中一样直接获取到。 defineExpose() 函数默认就在当前作用域中不需要导入。 // Home.vue
templateHome refhomeRef/Homebutton clickgetComponentData获取子组件中的数据/button
/templatescript setup
import {ref} from vue
import Home from ./components/Home.vue;const homeRef ref()
const getComponentData () {console.log(homeRef.value.message)
}
/scriptstyle scoped
/style // Home.vie
template
divHome/div
/templatescript setupconst message Hello Vue
// 组件中的数据、方法等如果想要其他组件能够通过其组件实例获取必须通过 defineExpose() 暴露出去
defineExpose({message,
})
/scriptstyle scoped
/style