2

I am building app using laravel and vue. I have navbar, currently it looks like:

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

And I use it like here:

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

In yield I am loading another components, so I have navbar and another component together. Now I want to override that {{msg}} variable from navbar in another components. In every component that variable will be diferent.

I do not know how to override it in components and from {{msg}} do some text. Can you help me? (That code above is all what I have)

2 Answers 2

1

If you want to use msg in other components, then you need to use prop

Use like:

props: ['msg'],

Then, you need to bind it like:

<component-name :msg="msg"></component-name>

In your component, you can take it like:

<template>{{ msg }}</template>

Hope you understand!

Sign up to request clarification or add additional context in comments.

24 Comments

Not this way. I have navbar and index for example. In navbar i have <p>{{msg}}</p>. Now when we are on index page that msg should turn into text for example 'index page'. I do not know how to do it. I do not want to add another html elements. Should be that what I have.
This is the trick of props what is right! I don't think it may turn into different text that you want!
Can u explain me more about the code you wrote? I don't understand where put it
Ok, use props in file from you export it, in same file <component-name :msg="msg"></component-name> and get {{ msg }} in any component you want! Please refer this: vuejs.org/v2/guide/components.html#Props
errors in navbar - 1. props must be strings when using array syntax. 2. 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
|
0

Components can be communicate with props. You can transfer the data to another components and you can use if statement.

https://v2.vuejs.org/v2/guide/components.html#Props

4 Comments

Should I do it in app.js?
No need that. You have to creat props on where ever you may want to use. And after you can take the data in a html tag like that :data="dataname" youtube.com/watch?v=PPmg7ntQjzc
But I don't want to add another html in my child components. Is there any way to override that variable in data() or something like this?
You are not adding any html tag. This is a component, <yield></yield> and after the props, it's happening like this <yield :props="props"></yield> like that. Yield component can be read what in side navbar, and you can act from this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.