Home > Article > Web Front-end > What is the difference between
<script> import { ref } from 'vue' export default { setup () { const num = ref(1); return { num } } } </script>
The variables and functions defined in the setup function need to be returned, otherwise they cannot be used normally.
<script setup> import { ref } from 'vue' const num = ref(1) </script>
Variables and functions defined in syntax sugar in
<script> import Hello from '@/components/HelloWorld' export default { components: { Hello } } </script>
<script setup> import Hello from '@/components/HelloWorld' </script>
No need to register in component, you can use it directly.
<template> <h2 v-onceClick>使用了setup函数</h2> </template> <script> export default { directives: { onceClick: { mounted (el, binding, vnode) { console.log(el) } } }, } </script>
<template> <h2 v-my-Directive>使用了script setup</h2> </template> <script setup> const vMyDirective = { beforeMount: (el) => { console.log(el) } } </script>
Globally registered custom instructions will work normally. Local custom instructions do not need to be explicitly registered in <script setup></script>
, but they must follow the naming convention of vNameOfDirective
<Com :num="100"></Com>
<script> export default { props: { num: { type: Number, default: 1 } }, setup (props) { console.log(props) } } </script>
<script setup> import { defineProps } from 'vue' const props = defineProps({ num: { type: Number, default: 1 } }) </script>
<script> export default { setup (props, context) { const sendNum = () => { context.emit('sendNum', 200) } return { sendNum } } } </script>
<script setup> import { defineProps, defineEmits } from 'vue' const emit = defineEmits(['submit']) const sendNum = () => { emit('submit', 1000) } </script>