Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JackJones's avatar

Passing props to component in composition api

I've built an app using the options api and now I'm learning the composition api, mainly because I need to reuse half the code elsewhere

I could pass props to a component inside the #app div using the options api like so:

import CalendarYear from "./CalendarYear.vue";

export default {
    components: { CalendarYear }
}
    <div id="app">
        <calendar-year
            route="{{ route('api.calendars.base-calendars.show', $calendar) }}"
            route-save="{{ route('api.calendars.base-calendars.edit', $calendar) }}"
        ></calendar-year>
    </div>

Is there a way to do the same in the comp api?

0 likes
9 replies
gych's avatar

This should also work in the Vue composition API

JackJones's avatar

@gych Maybe I should have been clearer

The

    <div id="app">
        <calendar-year
            route="{{ route('api.calendars.base-calendars.show', $calendar) }}"
            route-save="{{ route('api.calendars.base-calendars.edit', $calendar) }}"
        ></calendar-year>
    </div>

Is in my index.html

gych's avatar

@JackJones This should also work in the Composition API. Why do you think this won't work?

JackJones's avatar

Because CalendarxYear never loads

It loads if I put it inside the .vue file, but if I put it inside the <div id="app"> it doesn't load

I made a mistake in the original question, I'm using a .vue file to import

EDIT:

As a work-around I'm just going to use the options API to load the component, then use composition API for everything else

gych's avatar

@JackJones You mean options or composition API in app.js ? Did you define the component in app.js?

JackJones's avatar

@gych if I use the options API:

app.js:

import './bootstrap';
import { createApp } from "vue/dist/vue.esm-bundler";
import Calendar from './components/Calendarx.js';

Date.prototype.addDays = function (days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}

createApp(Calendar).mount("#calendar")

Then my Calendarx.js looks like:

import CalendarxYear from "./CalendarxYear.vue";

export default {
    components: { CalendarxYear }
}

and it works, no issues

What I wanted was to do the same using the composition API, so my app.js would look like:

import './bootstrap';
import { createApp } from "vue/dist/vue.esm-bundler";
import Calendar from './components/Calendarx.vue'; // using <script> Tags etc in Vue file

Date.prototype.addDays = function (days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}

createApp(Calendar).mount("#calendar")

And then I don't know what Calendarx.vue would look like, I just want to use a component purely so I can pass it props in my html view (I'm using Laravel and I need to pass in some routes as props)

gych's avatar
gych
Best Answer
Level 29

@JackJones Try to use this in app.js

import './bootstrap';
import { createApp } from "vue/dist/vue.esm-bundler";
import CalendarxYear from './components/CalendarxYear.vue'

const app = createApp()
app.component('calendar-year', CalendarxYear)
app.mount('#app')
JackJones's avatar

@gych that's the one

I was passing Calendarx to createApp and it wasn't working, having it empty fixes it, thank you so much it's been driving me wild

Plus it lets me get rid of an unneeded file

1 like

Please or to participate in this conversation.