• 父子组件传递参数/默认插槽/具名插槽


    父子组件传递参数 

    uni-app 中,自定义组件之间可以通过 props 和事件的方式进行参数传递。下面我将详细说明父子组件之间相互传递参数的方法:

    1. 父组件向子组件传递参数(使用 props):
      • 在子组件的 vue 文件中,通过 props 字段定义需要接收的参数。
      • 在父组件使用子组件的地方,通过绑定属性的方式将数据传递给子组件。

    子组件示例代码(Child.vue):

    1. <template>
    2. <div>
    3. <h2>{{ title }}h2>
    4. <p>{{ content }}p>
    5. div>
    6. template>
    7. <script>
    8. export default {
    9. props: {
    10. title: {
    11. type: String,
    12. required: true
    13. },
    14. content: {
    15. type: String,
    16. default: ''
    17. }
    18. }
    19. }
    20. script>

     父组件示例代码(Parent.vue):

    1. <template>
    2. <div>
    3. <child-component :title="title" :content="content">child-component>
    4. div>
    5. template>
    6. <script>
    7. import ChildComponent from '@/components/Child.vue';
    8. export default {
    9. components: {
    10. ChildComponent
    11. },
    12. data() {
    13. return {
    14. title: '标题',
    15. content: '内容'
    16. }
    17. }
    18. }
    19. script>

    1. 子组件向父组件传递参数(使用事件):
      • 在子组件中通过 $emit 方法触发一个自定义事件,并将需要传递的数据作为参数传入。
      • 在父组件中监听子组件触发的自定义事件,并在相应的方法中获取传递的参数。

    子组件示例代码(Child.vue):

    1. <template>
    2. <button @click="handleClick">点击触发事件button>
    3. template>
    4. <script>
    5. export default {
    6. methods: {
    7. handleClick() {
    8. const data = '这是子组件传递的数据';
    9. this.$emit('child-event', data);
    10. }
    11. }
    12. }
    13. script>

     父组件示例代码(Parent.vue):

    1. <template>
    2. <div>
    3. <child-component @child-event="handleChildEvent">child-component>
    4. <p>接收子组件传递参数:{{ childData }}p>
    5. div>
    6. template>
    7. <script>
    8. import ChildComponent from '@/components/Child.vue';
    9. export default {
    10. components: {
    11. ChildComponent
    12. },
    13. data() {
    14. return {
    15. childData: ''
    16. }
    17. },
    18. methods: {
    19. handleChildEvent(data) {
    20. this.childData = data;
    21. }
    22. }
    23. }
    24. script>

     通过上述方法,父组件和子组件就可以相互传递参数。父组件通过 props 将数据传递给子组件,子组件通过事件将数据传递给父组件,实现了双向的参数传递

    默认插槽 

    在 uni-app 中,插槽(slot)是一种用于在组件内部插入内容的机制。通过插槽,我们可以将外部传入的内容动态地插入到组件的指定位置。uni-app 支持两种类型的插槽:默认插槽和具名插槽。

    1. 默认插槽:

    默认插槽是最基本的插槽类型,它允许在组件中插入任意内容。在组件的模板中使用 标签来表示默认插槽的位置。

    组件示例代码(Child.vue):

    1. <template>
    2. <div>
    3. <h2>我是子组件h2>
    4. <slot>slot>
    5. div>
    6. template>
    7. <script>
    8. export default {
    9. }
    10. script>

     父组件示例代码(Parent.vue):

    1. template>
    2. <div>
    3. <child-component>
    4. <p>这是插入到子组件的内容p>
    5. child-component>
    6. div>
    7. template>
    8. <script>
    9. import ChildComponent from '@/components/Child.vue';
    10. export default {
    11. components: {
    12. ChildComponent
    13. }
    14. }
    15. script>

     在上述示例中,我们在父组件中使用了 标签,并在其中插入了一个

    标签。这个

    标签就会被动态地插入到子组件的默认插槽位置。

    具名插槽

    具名插槽允许我们在组件中定义多个插槽,并可以根据需要将内容插入到指定的插槽中。在组件的模板中使用 标签来表示具名插槽的位置。

    组件示例代码(Child.vue):

    1. <template>
    2. <div>
    3. <h2>我是子组件h2>
    4. <slot name="content">slot>
    5. <slot name="footer">slot>
    6. div>
    7. template>
    8. <script>
    9. export default {
    10. }
    11. script>

     父组件示例代码(Parent.vue):

    1. <template>
    2. <div>
    3. <child-component>
    4. <template v-slot:content>
    5. <p>这是插入到内容插槽的内容p>
    6. template>
    7. <template v-slot:footer>
    8. <button>提交button>
    9. template>
    10. child-component>
    11. div>
    12. template>
    13. <script>
    14. import ChildComponent from '@/components/Child.vue';
    15. export default {
    16. components: {
    17. ChildComponent
    18. }
    19. }
    20. script>

    在上述示例中,我们在子组件中定义了两个具名插槽:contentfooter。在父组件中,我们使用了