Coder Social home page Coder Social logo

Comments (2)

chengpeiquan avatar chengpeiquan commented on June 4, 2024 1

最近一段时间比较忙没怎么关注 3.0 的进展,script-setup 变化还挺大的,我回头再更新一下文档~

关于这 2 个 API,用法是这样:


useAttrs 可以直接拿到非 props 的数据

<script setup lang="ts">
import { useAttrs } from 'vue'

const attrs = useAttrs()

// 打印所有的attrs
console.log(attrs);

// 只打印某个attr
console.log(attrs.msg);
</script>

如果没有指定为 props,那么父组件传进来的属性都会挂到 attrs 这边来,就可以通过这个方法拿到了


useSlots 目前推测应该是提供给 JSX / TSX 用的,用法比如:

父组件如果是像这样传入了插槽数据

<template>
  <ChildTSX>
    <p>I am a default slot from TSX.</p>
    <template #msg>
      <p>I am a msg slot from TSX.</p>
    </template>
  </ChildTSX>
</template>

<script setup lang="ts">
import ChildTSX from '@cp/context/Child.tsx'
</script>

那么在 JSX / TSX 的子组件,通过 useSlots 来获取父组件传进来的 slots

import { defineComponent, useSlots } from 'vue'

const ChildTSX = defineComponent({
  setup() {
    const slots = useSlots()
    return () => (
      <div>
        <p>{ slots.default ? slots.default() : '' }</p>
        <p>{ slots.msg ? slots.msg() : '' }</p>
      </div>
    );
  },
});

export default ChildTSX;

from learning-vue3.

chengpeiquan avatar chengpeiquan commented on June 4, 2024

果然是实验性的 API ,还没正式启用就弃用了……我去瞅瞅新的变化~

from learning-vue3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.