1

I have a Laravel back-end where i can manage my Urls.

In front i'm using Vuejs with vue-router

First in main.js

    import EstarterSettings from './plugins/EstarterSettings'
    import router from './router'

then i'm trying to use a plugin to load my routes in the plugin

    import {RestDataSourcesMixin} from '../mixins/RestDataSourcesMixin'

    export default  {
        async install(Vue, options) {
        Vue.prototype.$routes = await 
                RestDataSourcesMixin.methods.requestApi(`/api/get-routes`)
        }
    };

Now, i want to use this routes in my vue-router

export default [
    {
        path: Vue.prototype.$routes.home,
        name: 'home',
        component: Views.Home,
        breadcrumb: []
    },
    {
        path: Vue.prototype.$routes.forgotten_password,
        name: 'forgottenPassword',
        component: Views.ForgottenPassword,
        meta: {
            requiresVisitor: true,
            breadcrumb: [
                { name: 'Home', link: '/' },
                { name: 'Forgotten password' }
            ]
        }
    }
]

But cause async, routes are undefined

How can i wait for routes loading or is there a way to do this ?

Thanks a lot

Edit

I was able to solve my problem thanks to this link. it exactly matches what I was looking for :

How to load all server side data on initial vue.js / vue-router load?

Edit 2

I found another way to do this. Set my routes as global variables in the html layout before the vuejs app

<script>
    window.appSettings = {
        routes : {{\app\Http\Controllers\Front\System\EstarterSettingController::getRoutes()}},
        settings: {{\app\Http\Controllers\Front\System\EstarterSettingController::getSettings()}}
    }
</script>
<script src="{{ mix('js/main.js') }}"></script>
3
  • Have you tried using .then() method? Commented Nov 20, 2019 at 7:25
  • stackoverflow.com/a/47302614/5962802 Commented Nov 20, 2019 at 8:55
  • @Ckuessner we can't put import in then() Commented Nov 21, 2019 at 17:41

0

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.