小编tsc*_*tao的帖子

如何将每个元素包装在一个插槽中?

我编写了一个组件(组件 B),它通过这样的插槽接受自定义组件列表

// Component A
<div class="component-a">
  ...
  <component-b>
    <component-x></component-x>
    <component-y></component-y>
  </component-b>
  ...
</div>
Run Code Online (Sandbox Code Playgroud)

我想将组件 x 和 y 包装在其他组件中,例如 li 标签。

// Output
...
<ul>
  <li><component-x></component-x></li>
  <li><component-y></component-y></li>
</ul>
...
Run Code Online (Sandbox Code Playgroud)

我试过

// Component B
<div class="component-b">
  <ul>
    <li v-for="item in $slots.default">
      <component :is="item"></component>
    </li>
  </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

它不起作用。该项目是 VNode 对象,无法使用组件标签进行渲染。有没有办法解决这个问题?

编辑:我的包装组件不是 li 标签,它是一个自定义组件,具有我在组件 B 中设置的指定道具。如果我从组件 A 包装它们,我需要重复编写自定义组件及其道具。

Edit2: Render 函数也许可以解决这个问题,但我正在寻找带有 html 模板(单文件组件)的解决方案。

vue.js vuejs2

5
推荐指数
2
解决办法
1983
查看次数

传播运营商取代价值?

为什么价差运营商会在这里用'HB woodlawn'取代'Stratford'的价值?这是如何运作的?

const editName = (oldName, name, arr) =>
  arr.map(item => {
    if (item.name === oldName) {
      return {
        ...item,
        name
      }
    } else {
      return item
    }
  })

let schools = [
  { name: "Yorktown"},
  { name: "Stratford" },
  { name: "Washington & Lee"},
  { name: "Wakefield"}
]
let updatedSchools = editName("Stratford", "HB Woodlawn", schools)
console.log( updatedSchools[1] ) // { name: "HB Woodlawn" }
console.log( schools[1] ) // { name: "Stratford" },
Run Code Online (Sandbox Code Playgroud)

javascript

1
推荐指数
1
解决办法
3870
查看次数

标签 统计

javascript ×1

vue.js ×1

vuejs2 ×1