简体   繁体   English

Vue 只呈现第一个自定义组件问题

[英]Vue only renders first custom component issue

I have a simple Vue app where I'm trying to render multiple custom components, here's what I'm trying:我有一个简单的 Vue 应用程序,我正在尝试渲染多个自定义组件,这就是我正在尝试的:

JS JS

Vue.component('refinement-list', {
  props: ['attribute', 'title'],
  template: '<div>{{attribute}} - {{title}}</div>'
});

new Vue({
  el: '#app'
});

HTML HTML

<div id="app">
  <refinement-list attribute="test" title="Test" />
  <refinement-list attribute="sample" title="Sample" />
</div>

However the problem is that only the first component is rendered, see working example: https://codepen.io/javiervd/pen/vYBpQMm但是问题是只渲染了第一个组件,请参见工作示例: https ://codepen.io/javiervd/pen/vYBpQMm

I tried registering the component locally instead but no luck.我尝试在本地注册组件,但没有运气。 I'm also using Vue with a script tag in my app because I can't use a build system for this particular project, not sure if that matters.我还在我的应用程序中使用带有脚本标签的 Vue,因为我不能为这个特定项目使用构建系统,不确定这是否重要。

Can anyone point me in the right direction?谁能指出我正确的方向? Thanks.谢谢。

Since you are defining the template in the DOM, you can't use self-closing tags for the custom components.由于您在 DOM 中定义模板,因此您不能对自定义组件使用自闭合标签。

This should work:这应该有效:

<div id="app">
  <refinement-list attribute="test" title="Test"></refinement-list>
  <refinement-list attribute="sample" title="Sample"></refinement-list>
</div>

This limitation doesn't exist in template strings or Single File Components.模板字符串或单个文件组件中不存在此限制。

You can read more about this here: https://v2.vuejs.org/v2/style-guide/#Self-closing-components-strongly-recommended您可以在此处阅读有关此内容的更多信息: https ://v2.vuejs.org/v2/style-guide/#Self-closing-components-strongly-recommended

You should give like this in the HTML section if you want to use the self-closing tags.如果你想使用自闭合标签,你应该在 HTML 部分这样给出。 But it's not recommended for DOM Templates但不推荐用于 DOM 模板

<div id="app">
  <div>
  <refinement-list attribute="test" title="Test1" />
  </div>
  <div>
  <refinement-list attribute="test" title="Test2" />
  </div>
</div>

在此处输入图像描述

I forked your codepen and can confirm this is working with the separate closing tag style, in place of using self-closing tags for the components.我分叉了你的 codepen,可以确认这是使用单独的结束标签样式,而不是使用组件的自结束标签。

<div id="app">
  <refinement-list attribute="test" title="Test"></refinement-list>
  <refinement-list attribute="sample" title="Sample"></refinement-list>
</div>

ref: https://codepen.io/edm00se/pen/pozpqym?editors=1010参考: https ://codepen.io/edm00se/pen/pozpqym?editors=1010

The self-closing tag versus (full?) closing tag is discussed in the vue style guide page (v2) and it is expressed that for string templates (as I suspect codepen is doing when loading your HTML content), to use the closing tag, while self-closing are fine in single-file components, JSX, and string templates (things which are processed during build and component names are better known).自闭合标签与(完整?)闭合标签在 vue 样式指南页面(v2)中进行了讨论,并表示对于字符串模板(正如我怀疑 codepen 在加载 HTML 内容时所做的那样),使用闭合标签,而自关闭在单文件组件、JSX 和字符串模板中很好(在构建过程中处理的东西和组件名称更广为人知)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM