javascript - 如果插槽不是从父级传递的,则进行条件渲染

标签 javascript vue.js vuejs2

以下代码为例:

<header>
  <slot name='header'></slot>
  <template v-if='?'>
    <h1>{{ someInput }}</h1>
  </template>
</header>

我想显示<h1>{{ some input }}</h1>如果没有任何东西通过插槽。我可以使用什么作为 v-if 中的条件?

最佳答案

这就足够了:

<header>
  <slot name='header'>
    <h1>{{ someInput }}</h1>
  </slot>
</header>

Anything originally inside the tags is considered fallback content. Fallback content is compiled in the child scope and will only be displayed if the hosting element is empty and has no content to be inserted.

来自docs .

关于javascript - 如果插槽不是从父级传递的,则进行条件渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42974854/

相关文章:

JavaScript 对象和 css 类出现问题

javascript - v-toolbar-title 中的字体大小未更新

vue.js - 可重复使用的嵌套 VueJS 组件

javascript - 在 Vue Client 3 中使用 webpack 插件

javascript - Vue.js 当只有一个根节点时存在多个根节点

javascript - 如何使用vuejs2从另一个组件执行功能

javascript - Chrome 扩展 NativeMessaging 'connectNative' 未定义

javascript - 如何在js中找到 Canvas 的中心

javascript - React prop 的本地副本是只读的

vue.js - 通过参数传递的 id 在页面刷新时丢失