Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register local component when using vue-property-decorator

Just learning Vue and TypeScript with class based components, my single file component looks like this:

<template>
    ...
    <div>
        <contact-component v-bind:key="c.id" v-for="c in contacts">
        </contact-component>
    </div>
</template>

<script lang="ts">
import ContactComponent from "./Contact.vue";
import { Component, Vue } from 'vue-property-decorator'

@Component
export default class ContactsComponent extends Vue {
    data() {...}
    components = { ContactComponent }
}
</script>

It generates the following error:

Unknown custom element: - did you register the component correctly?

Obviously my registration of the component does not work, but I have no idea how to fix this in TypeScript. What's the correct way to register ContactComponent so that it can be used in the above template?

like image 280
Achim Avatar asked Aug 09 '19 22:08

Achim


People also ask

Where do I register for Vue component?

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.

How do I declare a component in Vue?

Components with inline templates strings are also another way to declare Vue components. We can declare these components with the Vue. component method by passing in the name of the component as a string as the first argument, and the component object itself, including the template, in the 2nd argument.

What is Vue property decorator?

vue-property-decorator allows you to define your properties directly on your class with a simple @Prop() decorator. This approach allows your classes to stay clean and flat compared to the traditional approach of defining a props object with each prop defined on it.

How do I import a component into Vue?

STEP 01: First, Import the Child Component into the Parent Component inside script tag but above export default function declaration. STEP 02: Then, Register the Child Component inside the Parent Component by adding it to components object. STEP 03: Finally, Use the Child Component in the Parent Component Template.

How do I register a Vue component?

Read that first if you are new to components. A Vue component needs to be "registered" so that Vue knows where to locate its implementation when it is encountered in a template. There are two ways to register components: global and local. We can make components available globally in the current Vue application using the app.component () method:

What is missing in Vue property decorator?

Some of the most important parts of Vue are missing in Vue Class Component, for example, ECMAScript decorators like props, inject, model, ref, emit, and provide. As a result, the Vue community introduced a library called Vue Property Decorator, which fully depends on the Vue Class Component and is now fully endorsed by the Vue core team.

How to make components available globally in the current Vue application?

We can make components available globally in the current Vue application using the app.component () method: import { createApp } from 'vue' const app = createApp({}) app.component( // the registered name 'MyComponent', // the implementation { /* ... */ } ) If using SFCs, you will be registering the imported .vue files:

How to import a component from another component in Vue?

import ComponentA from './ComponentA.vue' export default { components: { ComponentA } // ... } You could do it simply as before by adding that component to components option : import ComponentA from './ComponentA.vue' export default defineComponent ( { setup () { const count = ref (0) return { count } }, components: { ComponentA } });


Video Answer


1 Answers

like this


@Component({
  components: { ContactComponent }
})
export default class ContactsComponent extends Vue {}
like image 171
Jacob Goh Avatar answered Sep 22 '22 03:09

Jacob Goh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!