简体   繁体   中英

Using props to set different text on every page in vue.js

I am using laravel + vue. I want to do title of page in navbar, so when you are in index, in navbar is text index. When you are in settings, navbar says settings etc.

I think props is good for it, but when I use that, it works not good. Look here:

blade.php of index:

@extends('layout.app')

@section('content')
    <index :msg="msg"></index>
@endsection

index.vue:

props: [
            'msg'
        ],

Now navbar:

<template>
<nav class="navbar">
        <a></a>
        <p>{{msg}}</p>
</nav>
</template>

<script>
    export default {
        props: [
         ],
        data() {
            return {
            }
        },

    }
</script>

and layout:

 <body>
    <div id="app">
        <navbar></navbar>
        @yield('content')
    </div>
</body>

How I can change that {{msg}} paragraph in navbar when we are on different pages? My code doesn't work.

[Vue warn]: Property or method "msg" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

If you want to use a prop then you need to define it in the props object of your component. In your NavBar you are referencing to msg, but the props object in NavBar is empty. Define it and pass the prop in your layout.blade.php.

Or you could also define a computed property where you take a look at the current route and return a string fitting your business. https://v2.vuejs.org/v2/guide/computed.html

If you want to share data between multiple components, then use a store (VUEX) as proposed :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM