1

So i am trying to fetch data in my vue component as you can see below. But because iam reqesting the fetch from my blade view in public/manage-users-projects the url becomes

http://localhost:8080/ProjsiteWebApp/app/public/manage-users-projects/get-projects-json

but i want to fetch data from this url

http://localhost:8080/ProjsiteWebApp/app/public/get-projects-json

So i can i sort of return one route and execute from the public folder?

export default {


    created(){
        this.fetchProjects();
    },


    methods:{
        fetchProjects() {
            fetch('get-projects-json')
                .then(res => res.json())
                .then(res => {
                    console.log(res.data);
                })
        }
    }
    
    

};






Route::get('get-projects-json', 'ProjectsController@getProjectsJson');
2
  • Why is this part of route needed: "ProjsiteWebApp/app/public" ? did you add it on purpose as a prefix ? Commented Nov 1, 2020 at 13:21
  • that is the exact url that the fetch methods calls on based on my parameter. Commented Nov 1, 2020 at 13:29

1 Answer 1

1

You can pass the route as props to the component inside the blade file.

Inside the view:

<component get-projects-json="{{ url('get-projects-json') }}"></component>

Inside the vue component:

<script> 
    export default {
        props: ['getProjectsJson'],
    }
</script>

Then you can access the route inside the component like so:

export default {


    created(){
        this.fetchProjects();
    },


    methods:{
        fetchProjects() {
            fetch(this.getProjectsJson)
                .then(res => res.json())
                .then(res => {
                    console.log(res.data);
                })
        }
    }
    
    

};

You can learn more about props here: https://v2.vuejs.org/v2/guide/components-props.html

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

Comments

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.