Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Personal Vue 3 component package missing template or render function

I recently uploaded my own Vue 3 component to NPM to make it usable for others. When using it in other projects it gives this warning:

[Vue warn]: Component is missing template or render function. 
at <VueToggle id="1" title="Toggle me" > 
at <App>

What could be the reason this is happening? The way I am building and publishing the app is by running this code.

import VueToggle from "./components/VueToggle";

export default {
    install(app) {
        app.component("vue-toggle", VueToggle);
    }
};

And then running this build command in my package.json:

"build-library": "vue-cli-service build --target lib --name vue-toggle-component ./src/install.js",

How I use my component in a different project:

<template>
  <VueToggle id="1" title="Toggle me"/>
</template>

<script>
import VueToggle from 'vue-toggle-component';

export default {
  name: 'App',
  components: {
    VueToggle,
  }
}
</script>

The component itself:

<template>
  <section class="wrapper" :class="{dark: darkTheme}" :title="title">
    <input
      :id="id"
      :name="name"
      v-model="toggleState"
      class="toggle"
      type="checkbox"
      @click="toggleState = !toggleState"
    />
    <label :for="id" class="toggler" :style="[toggleState && {'background': activeColor}]"/>
    <span class="title" v-text="title" @click="toggleState = !toggleState"/>
  </section>
</template>

<script>
export default {
  name: 'VueToggle',
  props: {
    activeColor: {type: String, default: '#9FD6AE'},
    darkTheme: {type: Boolean, default: false},
    id: {type: String, required: true},
    name: {type: [String, Boolean], default: false},
    title: {type: String, required: true},
    toggled: {type: Boolean, default: false},
  },
  data() {
    return {
      toggleState: this.toggled
    }
  },
}
</script>

The package: https://www.npmjs.com/package/vue-toggle-component

like image 833
Niels Bosman Avatar asked Oct 22 '25 23:10

Niels Bosman


1 Answers

The following concerns a new project using Vue 3 & Typescript created with Quasar CLI (v2 beta). Although I'm getting the same error reported by the OP, my source layout might be different as I'm not using single-file components.

[Vue warn]: Component is missing template or render function.

I resolved the above issue by specifying the vue file as the component source. I typically split my components into separate vue and ts files.

The related fragment:

MyComponent: require("./components/My.vue").default

In context:

export default defineComponent({
  name: "App",
  components: {
    MyComponent: require("./components/My.vue").default
  },
  setup() {
  ...

To quiet the linters, I have the following es-lint comments

export default defineComponent({
  name: "App",
  components: {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-var-requires
    MyComponent: require("./components/My.vue").default
  },

Ideally, the import statement would be used instead of the inline require assignment.

like image 145
bvj Avatar answered Oct 24 '25 13:10

bvj