question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I use the `route` function in a compiled Vue component?

See original GitHub issue

I’m using the following code:

                <div class="list-group" v-if="projects.length > 0">
                    <a v-for="project in projects" :href="route('api.project', {project_id: project.id})" class="list-group-item">{{ project.name }}</a>
                </div>

But I get the following error in console:

image

Note that both route and namedRoutes exist. I have @route above the app.js line in my layout.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:27 (9 by maintainers)

github_iconTop GitHub Comments

49reactions
Archer70commented, Feb 28, 2018

I know this issue is old, but I came across it by Googling the same problem and thought I might add my solution as well. I was able to add route as a global function to all my compiled Vue components by adding this to app.js

Vue.mixin({
    methods: {
        route: route
    }
});

And then you can call it like so:

<a class="nav-link" :href="route('home')">{{ $t('Home') }}</a>
6reactions
simensencommented, Sep 5, 2017

@stevebauman Great, thanks. I decided to use a function instead as that appears to work OK:

                <div class="list-group" v-if="projects.length > 0">
                    <a v-for="project in projects" :href="projectUrl(project)" class="list-group-item">{{ project.name }}</a>
                </div>

<script>
    export default {
        methods: {
            projectUrl(project) {
                return route('api.project', {project_id: project.id})
            },
...

Indeed, it would be nice to be able to use this the other way, though. I can keep this issue open as a feature request or I can close it. I’m happy that I have a solution now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started - Vue Router
Define some routes // Each route should map to a component. // We'll talk about nested routes later. const routes = [ {...
Read more >
How to use routing in Vue.js ? - GeeksforGeeks
Step 1: Vue Router can be installed through Npm with the package named vue-router using below command. npm install vue-router // OR vue...
Read more >
Vue: Automate Your Routing - Medium
In this article, you will learn how to automate your Vue Router route definitions using Node and webpack and without the help of...
Read more >
vue.js - VueJS: How to navigate to a different route from global ...
Probably the best approach is to register a hook on the window object and use that so you can navigate from inside Vue....
Read more >
How to use routing in Vue.js to create a better user experience
The $router method takes two parameters: first, the name of the component we want to navigate to, and second, the id parameter (or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found