简体   繁体   English

为什么在使用自定义 HTML 标签时在 Vue 中出现错误?

[英]Why I get an error in Vue when using custom HTML tags?

I really enjoy Vue I'm a newbie trying to use just a custom tag:我真的很喜欢 Vue 我是一个新手,只尝试使用自定义标签:

<ui-button>Learn more</ui-button>

However I get an error saying that I have to register that component.但是我收到一条错误消息,说我必须注册该组件。 So, to solve this I used: https://v2.vuejs.org/v2/api/#ignoredElements所以,为了解决这个问题,我使用了: https://v2.vuejs.org/v2/api/#ignoredElements

I tried in other frameworks too and not errors are shown but other frameworks follow different approach.我也在其他框架中尝试过并且没有显示错误,但是其他框架遵循不同的方法。 Maybe is not possible to distinguish between a component and a custom tag.也许无法区分组件和自定义标签。

custom component = custom tag + encapsulated behavior and style自定义组件 = 自定义标签 + 封装的行为和样式

Is possible to avoid this in Vue without registering my tags as components?如果不将我的标签注册为组件,是否可以在 Vue 中避免这种情况?

Update更新

Thank you so much for your time and efforts and please I'm not saying other frameworks are better.非常感谢您的时间和努力,我并不是说其他框架更好。 I just wanna avoid over-engineering and use simple custom tags to make the HTML document more readable.我只是想避免过度设计并使用简单的自定义标签来使 HTML 文档更具可读性。

You should register components globally or locally.您应该在全局或本地注册组件。

Global registration全球注册

Vue.component('my-component-name', {
  // ... options ...
})

Local registration (in another component)本地注册(在另一个组件中)

import ComponentA from './ComponentA.vue'

export default {
  components: {
    ComponentA
  },
  // ...
}

More information about that https://v2.vuejs.org/v2/guide/components-registration.html关于https://v2.vuejs.org/v2/guide/components-registration.html的更多信息

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

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