Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data to a router-view in Vue.js

How can I pass data from my main app to a component through router-view in Vue.js? I have successfully gotten the data from my API as shown below:

mounted() {
    // console.log(model)
    this.model = model;
    // console.log(this.model)
}

The component I want to pass data to has been loaded as shown below:

@section('content')
<div style="padding: 0.9rem" id="app">
    <router-view name="bookBus"></router-view>
    <router-view></router-view>
    {{-- @{{ model }} --}}
</div>

@stop

How do I pass the model data to the bookBus component?

like image 424
Dapo Michaels Avatar asked Apr 06 '18 15:04

Dapo Michaels


People also ask

Can you pass Props to router-view Vue?

Vue Router 4 provides multiple ways of doing this: from setting the props property on the route record to true and automatically passing all params as props, setting the props property to a static object to provide static props, or setting it to a function that returns the desired props.

How do I transfer data to Vue props?

To specify the type of prop you want to use in Vue, you will use an object instead of an array. You'll use the name of the property as the key of each property, and the type as the value. If the type of the data passed does not match the prop type, Vue sends an alert (in development mode) in the console with a warning.


1 Answers

From https://router.vuejs.org/en/api/router-view.html

Any non-name props will be passed along to the rendered component, however most of the time the per-route data is contained in the route's params.

So if you want to pass data down from the parent component, rather than from the router, it would be something like this:

<router-view name="bookBus" :model="model"></router-view>

Your bookBus component would then need to have a model prop configured to receive the data, just like it would for any other prop.

like image 127
skirtle Avatar answered Oct 01 '22 18:10

skirtle