Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue component name must be lowercase?

Tags:

vuejs2

I am trying to use a component in my view file. The following doesn't work When I try to mount the component in my view with <CampaignCreate></CampaignCreate>

const app = new Vue({
    el: '#rewards-app',
    components: {
       CampaignCreate,
    }
});

If I change it to:

const app = new Vue({
    el: '#rewards-app',
    components: {
       'campaign-create': CampaignCreate,
    }
});

I can mount the component in my view file as <campaign-create></campaign-create> without a problem. I am trying to understand the reason behind this. I am currently on vuejs 2.x

like image 945
Gayan Hewa Avatar asked Aug 29 '18 20:08

Gayan Hewa


People also ask

Should Vue components be capitalized?

From what I have seen and used, vuejs doesn't mind your component files', component names' case. Note that Vue does not enforce the W3C rules for custom tag names (all-lowercase, must contain a hyphen) though following this convention is considered good practice.

How do I name my Vue components?

Component names should always be multi-word, except for root App components, and built-in components provided by Vue, such as <transition> or <component> . This prevents conflicts with existing and future HTML elements, since all HTML elements are a single word.

Are Vue props case sensitive?

Prop Casing (camelCase vs kebab-case)HTML attribute names are case-insensitive, so browsers will interpret upper-case characters as lower-case. This means that when they're used directly in HTML, camelCased prop names have to be changed to their kebab-case equivalent.

How do I register a component in VUE JS?

When we want to globally register a component in Vue, we need to do it in this file. All you have to do is import your component, as you usually would, and then register it using app. component . import { createApp } from 'vue' import App from './App.


1 Answers

In short, it's because HTML is case-insensitive. There was a big discussion in VueJS tracker opened 2 years ago by Evan You himself with the following reasoning:

So as we all know, HTML is case insensitive. myProp="123" gets parsed as myprop="123" and this has led to the caveat in Vue.js where you have to use my-prop="123" to refer to a prop declared in JavaScript as myProp. This bites beginners quite often.

The issue was eventually closed with decision to stay on the same track. Here's a telling quote:

Essentially, the problem exists because js and html are different technologies and use different naming systems. And using same case(kebab or camel) in both technologies will shift weirdness from one place to another but the underlying problem will persist So I believe, best we can do is draw a line. and the current line i,e. kebab case in html context and camelCase (and PascalCase) in js context is very good.

like image 77
raina77ow Avatar answered Sep 28 '22 23:09

raina77ow