Vue-router: Route Segments and 404's

Created on 10 Nov 2015  路  6Comments  路  Source: vuejs/vue-router

Hello all,

Vue is awesome, really loving 1.0! I have two questions (_I'll explain what my issue is below after I have shown my existing code_).

  • Q1. Why does my component not fire the ready() method each time the segment changes?
  • Q2. How can I handle 404's for a resource that does not exist?

My routes look like this:

// Views are imported

router.map({
    '/':                        { component: HomeView },
    '/category/:category':      { component: CategoryView },
    '*':                        { component: NotFoundView }
});

My CategoryView looks like this...

<template>
    <h1>{{ category.name }}</h1>
    <hr>
    <div class="row products">
        <div class="col-sm-4" v-for="product in category.products.data">
            <product :product="product"></product>
        </div>
    </div>
</template>

<script>
import Product from "../components/Product.vue";

export default {

    name: 'CategoryView',

    props: {
        category: Object
    },

    components: {
        Product
    },

    ready: function() {
        this.getCategory();
    },

    methods: {

        getCategory: function() {
            this.$http.get('/api/categories/' + this.$route.params.category + '?include=products', function(category) {

                // Set Category
                this.$set('category', category.data);

            }).error(function(resp) {

                // Category does not exist

            });
        }

    }

}
</script>

Q1. In all of my views, I have my navigation which includes all of my categories. When I select the first category, the ready() function gets fired and fetches the category and it's products.

My problem is, if I select another category from the navigation - the active link changes - but my view does not change, updating the category name and it's products.

Here's my example (screencast):

IMAGE ALT TEXT

You can see the issue at 12 seconds.

Q2. Finally, if I was to access a category that does not exist. Let's say /category/doesNotExist - how can I "throw" my NotFoundView here?

I look forward to hearing from you - I appreciate any help!

Most helpful comment

@amirrustam how ironic, http://router.vuejs.org/en/api/go.html is a 404.

All 6 comments

If you want to react to route changes then you should be using the route hooks: http://router.vuejs.org/en/pipeline/index.html

Hey @yyx990803, do you know of any working examples? Fairly new to Vue still :)

After taking a look, I understand it now I think. I'm using this:

route: {
    canReuse: function (transition) {
        transition.to
    }
}

Not entirely sure if that's correct way to do it, but it works :smiley:

Lastly then, how would I throw a 404 for an invalid category in this case?

@JoeDawson to answer your second and last question you can use router.go to direct to a 404 route. Here is the documentation: http://vuejs.github.io/vue-router/en/api/go.html

If all your questions are answered then please close out this issue. In the future, these type of questions are best suited for the community forums, gitter, or stackoverflow. Please review the Issue Reporting Guidelines.

Oh I'm way past this now, thanks for the response though @amirrustam.

@amirrustam how ironic, http://router.vuejs.org/en/api/go.html is a 404.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rr326 picture rr326  路  3Comments

marcvdm picture marcvdm  路  3Comments

thomas-alrek picture thomas-alrek  路  3Comments

saman picture saman  路  3Comments

lbineau picture lbineau  路  3Comments