Vue3中的setup与自定义指令怎么使用

其他教程   发布日期:2025年02月21日   浏览次数:179

本篇内容介绍了“Vue3中的setup与自定义指令怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

setup语法糖

最大好处就是所有声明部分皆可直接使用,无需return出去

注意:部分功能还不完善,如:name、render还需要单独加入script标签按compositionAPI方式编写

// setup 下还可以附加<script>

setup语法糖独有

  1. <script setup>
  2. import { ref ,reactive,toRefs } from 'vue'
  3. const a = 1;
  4. const num = ref(99) // 基本数据类型
  5. const user = reactive({ // 引用数据类型
  6. age:11
  7. })
  8. // 解构能获取响应式属性 {}解构 toRefs保留响应式
  9. const { age } = toRefs(user)
  10. // 导出
  11. defineExpose({
  12. a
  13. })
  14. // props
  15. const props = defineProps({
  16. foo: String
  17. })
  18. // 事件
  19. const emit = defineEmits(['change', 'delete'])
  20. // 自定义指令
  21. const vMyDirective = {
  22. created(el, binding, vnode, prevVnode) {
  23. // 下面会介绍各个参数的细节
  24. console.log('创建了')
  25. },
  26. }
  27. </script>

defineProps defineEmits与组件应用

  1. // 子组件
  2. <template>
  3. <div class="hello">
  4. <h2>{{ msg }}</h2>
  5. <slot name="btn">
  6. </slot>
  7. <button @click="chickMe"></button>
  8. </div>
  9. </template>
  10. <script setup>
  11. import { useSlots, useAttrs } from 'vue';
  12. const slots = useSlots()
  13. const attrs = useAttrs()
  14. const props = defineProps({
  15. msg: String
  16. })
  17. const emit = defineEmits(['change'])
  18. console.log(slots, attrs)
  19. const chickMe = ()=>{
  20. emit('change','abc')
  21. }
  22. </script>
  23. // 父组件
  24. <template>
  25. <div class="home" >
  26. <HelloWorld msg="hello" atr="attrs" @change="changeP ">
  27. <template #btn>
  28. <div>我是 btn:{{ obj.text }}</div>
  29. </template>
  30. </HelloWorld>
  31. </div>
  32. </template>
  33. <script setup>
  34. import HelloWorld from '../components/HelloWorld.vue';
  35. import { ref ,reactive,toRefs } from 'vue'
  36. const obj = reactive({
  37. id: 0,
  38. text: '小红'
  39. })
  40. const changeP=(e)=>{
  41. console.log(e)
  42. }
  43. </script>

defineExpose与组件应用

  1. // 子组件
  2. <template>
  3. <div class="hello">
  4. 123
  5. </div>
  6. </template>
  7. <script setup>
  8. const testPose =()=>{
  9. console.log('子组件暴露方法')
  10. }
  11. defineExpose({
  12. testPose
  13. })
  14. </script>
  15. // 父组件
  16. <template>
  17. <div class="home" v-test>
  18. <HelloWorld ref="helloSon"></HelloWorld>
  19. <button @click="testEpose"></button>
  20. </div>
  21. </template>
  22. <script setup>
  23. import HelloWorld from '../components/HelloWorld.vue';
  24. import { ref } from 'vue'
  25. // setup函数的话可以从context上查找
  26. const helloSon = ref(null);
  27. const testEpose = () => {
  28. helloSon.value.testPose();
  29. }
  30. </script>

自定义指令相关

  • created:在绑定元素的 attribute 或事件监听器被应用之前调用。在指令需要附加在普通的 v-on 事件监听器调用前的事件监听器中时,这很有用。

  • beforeMount:当指令第一次绑定到元素并且在挂载父组件之前调用。

  • mounted:在绑定元素的父组件被挂载后调用,大部分自定义指令都写在这里。

  • beforeUpdate:在更新包含组件的 VNode 之前调用。

  • updated:在包含组件的 VNode 及其子组件的 VNode 更新后调用。

  • beforeUnmount:在卸载绑定元素的父组件之前调用

  • unmounted:当指令与元素解除绑定且父组件已卸载时,只调用一次。

  1. import { createApp } from 'vue';
  2. const Test = createApp();
  3. Test.directive('my-directive', {
  4. // 在绑定元素的 attribute 前
  5. // 或事件监听器应用前调用
  6. created(el, binding, vnode, prevVnode) {
  7. // 下面会介绍各个参数的细节
  8. console.log('创建了')
  9. },
  10. // 在元素被插入到 DOM 前调用
  11. beforeMount(el, binding, vnode, prevVnode) { },
  12. // 在绑定元素的父组件
  13. // 及他自己的所有子节点都挂载完成后调用
  14. mounted(el, binding, vnode, prevVnode) { },
  15. // 绑定元素的父组件更新前调用
  16. beforeUpdate(el, binding, vnode, prevVnode) { },
  17. // 在绑定元素的父组件
  18. // 及他自己的所有子节点都更新后调用
  19. updated(el, binding, vnode, prevVnode) { },
  20. // 绑定元素的父组件卸载前调用
  21. beforeUnmount(el, binding, vnode, prevVnode) { },
  22. // 绑定元素的父组件卸载后调用
  23. unmounted(el, binding, vnode, prevVnode) { }
  24. })
  25. export default Test.directive('my-directive');
    1. el
    :指令绑定到的元素。这可以用于直接操作 DOM。
    1. binding
    :一个对象,包含以下属性。
      1. value
      :传递给指令的值。例如在
      1. v-my-directive="1 + 1"
      中,值是
      1. 2
      1. oldValue
      :之前的值,仅在
      1. beforeUpdate
      1. updated
      中可用。无论值是否更改,它都可用。
      1. arg
      :传递给指令的参数 (如果有的话)。例如在
      1. v-my-directive:foo
      中,参数是
      1. "foo"
      1. modifiers
      :一个包含修饰符的对象 (如果有的话)。例如在
      1. v-my-directive.foo.bar
      中,修饰符对象是
      1. { foo: true, bar: true }
      1. instance
      :使用该指令的组件实例。
      1. dir
      :指令的定义对象。
    1. vnode
    :代表绑定元素的底层 VNode。
    1. prevNode
    :之前的渲染中代表指令所绑定元素的 VNode。仅在
    1. beforeUpdate
    1. updated
    钩子中可用。

应用

  1. <template>
  2. <div class="home" v-test>
  3. </div>
  4. </template>
  5. //setup 外部调用
  6. <script>
  7. // 指令必须 vXxx 这样书写
  8. import vTest from './TestDirective'
  9. export default defineComponent({
  10. directives: {
  11. test:vTest,
  12. },
  13. setup(props) {
  14. // console.log('Test',vTest)
  15. return {
  16. };
  17. }
  18. })
  19. </script>
  20. //或者 setup内部
  21. <script setup>
  22. import vTest from './TestDirective'
  23. </script>

对象字面量

  1. <div v-demo="{ color: 'white', text: 'hello!' }"></div>
  2. app.directive('demo', (el, binding) => {
  3. console.log(binding.value.color) // => "white"
  4. console.log(binding.value.text) // => "hello!"
  5. })

以上就是Vue3中的setup与自定义指令怎么使用的详细内容,更多关于Vue3中的setup与自定义指令怎么使用的资料请关注九品源码其它相关文章!