微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么 v 槽在单个文件中不起作用?

如何解决为什么 v 槽在单个文件中不起作用?

我想构建一个这样的组件:

<v-row>
  <v-col cols="6"> this is first Box: <slot name="first" /></v-col>
  <v-col cols="6"> this is second Box: <slot name="second" /></v-col>
  <template v-slot:first> something first.... </template>
  <template v-slot:second> something second.... </template>
</v-row>

但它返回一个错误

<template v-slot> can only appear at the root level inside the receiving component

我怎样才能让它工作?

------添加:------

请告诉我如何以这种方式构建组件:

<v-row>
  <v-col cols="6"> this is first Box: 
    <SomethingLikeSlot name="first" />
  </v-col>
  <v-col cols="6"> this is second Box: 
    <SomethingLikeSlot name="second" />
  </v-col>
  <template somethingLikev-slot="first"> something first.... </template>
  <template somethingLikev-slot="second"> something second.... </template>
</v-row>

我只想构建一个组件并制作上面所有的结构(请参阅顶部的 v-col)并将内容放在下面。 我不喜欢这种构建组件的方式,因为它会使布局(v-col)彼此相距太远:

<v-row>
  <v-col cols="6"> this is first Box: 
    <template > something very long....
    1
    2
    3
    6
    ...
    </template>
  </v-col>
  <v-col cols="6"> this is second Box: 
    <template > something second very long.... </template>
  </v-col>
</v-row>

但是现在slot和v-slot似乎只需要构建两个组件来应对这种情况。我可以只构建一个组件来实现这个目标吗?

解决方法

您没有正确使用插槽。我不确切知道您想要实现什么,但应该在使用您的自定义组件的父组件中使用 <template v-slot>

例如,如果您有此组件,我们将其命名为 foo

<v-row>
  <v-col cols="6"> this is first box: <slot name="first" /></v-col>
  <v-col cols="6"> this is second box: <slot name="second" /></v-col>
</v-row>

所以在你的父组件中,我们称之为parentFoo

<div>
  <foo>
     <template v-slot:first> something first.... </template>
     <template v-slot:second> something second.... </template>
  </foo>
</div>

这意味着 <template v-slot:first> 里面的内容会被渲染在里面

<v-col cols="6"> this is first box: <slot name="first" /></v-col>

所以渲染结果将是:

<v-col cols="6"> this is first box: something first....</v-col>

您可以在 vue docs

中阅读有关 v-slot 的更多信息

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?