vue.js - Vue.js - 命名槽
问题描述
我有一个关于命名插槽的初学者问题。在我的示例中,我只想显示第一个插槽的内容 - 但除非我<slot></slot>
不使用name="first"
. 我的例子有什么问题?
Vue.component('user-name', {
props: ['name'],
template: `
<p slot="first">Hi {{ name }}</p>
`
});
Vue.component('user-nickname', {
props: ['name'],
template: `
<p slot="secont">Byee {{ name }}</p>
`
});
Vue.component('user-information', {
template: `<div class="user-information">
<slot name="first"></slot>
</div>
`,
});
new Vue({
el: "#app"
});
body{
width:700px;
margin:0 auto;
text-align:center;
color: blue;
}
.user-information {
background: red;
width: 700px;
height: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<user-information>
<user-name name="Foo"></user-name>
<user-nickname name="Boo"></user-nickname>
</user-information>
</div>
解决方案
slot="..whatever.."
从您的组件中删除。然后将第二个命名槽添加到user-information
. 最后在您的主要参考中,插槽作为子组件的父级
<user-information>
<template v-slot:first>
<user-name name="Foo"></user-name>
</template>
<template v-slot:second>
<user-nickname name="Boo"></user-nickname>
</template>
</user-information>
推荐阅读
- python - Tornado 服务器在多进程模式下处理不平均的请求
- java - SpringBoot 应用程序:CPU 使用率有时会达到最大值
- google-cloud-dataflow - 在 Google Dataflow 上出现 503 错误 - 不可用
- java - 线程“主”java.time.format.DateTimeParseException 中的异常
- npm - IBM 区块链平台 Visual Studio Code:打包智能合约时出错
- angular - 如何在 Angular 中使用 JSON 模式创建动态选择列表
- linux - 如何为 DPDK 和 malloc 选择大页面大小?
- regex - 正则表达式大写仅用于 2 个字符
- ruby-on-rails - Rails 检查文本中的多个单词作为查询
- neo4j - 使用任意长文章文本查询 lucene 索引以检查文章中的所有匹配项(通过 neo4j)