Skip to content

Commit e796dc7

Browse files
committed
Added hooks
1 parent 11eb36a commit e796dc7

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

src/Cart.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@
6262
6363
export default {
6464
props: ['cart'],
65-
mixins: [ CartMixin ]
65+
mixins: [ CartMixin ],
66+
beforeRouteLeave(to, from, next) {
67+
if (this.cart.items.length > 0) {
68+
if (!confirm('Are you sure you don\'t want to buy these products?')) {
69+
return next(false);
70+
}
71+
}
72+
73+
next();
74+
}
6675
}
6776
</script>

src/ViewProduct.vue

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,17 @@
2727
};
2828
},
2929
created() {
30-
this.$watch('$route.query.discount', (newValue, oldValue) => {
31-
this.discount = this.getDiscount(this.product.price, newValue);
32-
});
33-
3430
this.product = this.getProduct(this.productId);
3531
3632
if (typeof this.$route.query.discount !== 'undefined') {
3733
this.discount = this.getDiscount(this.product.price, this.$route.query.discount);
3834
}
3935
},
40-
watch: {
41-
productId(newValue, oldValue) {
42-
this.product = this.getProduct(newValue);
43-
this.discount = this.getDiscount(this.product.price, this.$route.query.discount);
44-
}
36+
beforeRouteUpdate(to, from, next) {
37+
this.discount = this.getDiscount(this.product.price, to.query.discount);
38+
this.product = this.getProduct(to.params.productId);
39+
40+
next();
4541
},
4642
methods: {
4743
getProduct(productId) {

src/ViewProfile.vue

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
<template>
22
<h1>My Profile</h1>
3-
</template>
3+
</template>
4+
5+
<script>
6+
import { authService } from './main';
7+
8+
export default {
9+
beforeRouteEnter(to, from, next) {
10+
if (!authService.isLoggedIn) {
11+
alert("You must be logged in!");
12+
return next(false);
13+
}
14+
15+
next();
16+
}
17+
}
18+
</script>

src/routes.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Product from './Product.vue';
55
import ProductReviews from './ProductReviews.vue';
66
import SpecialOffer from './SpecialOffer.vue';
77
import ViewProfile from './ViewProfile.vue';
8-
import { authService } from './main';
98

109
export const routes = [
1110
{ path: '', components: {
@@ -19,15 +18,7 @@ export const routes = [
1918
{
2019
path: '/user/profile',
2120
name: 'viewProfile',
22-
component: ViewProfile,
23-
beforeEnter(to, from, next) {
24-
if (!authService.isLoggedIn) {
25-
alert("You must be logged in!");
26-
return next(false);
27-
}
28-
29-
next();
30-
}
21+
component: ViewProfile
3122
},
3223
{ path: '/cart', component: Cart },
3324
{ path: '*', component: { template: '<h1>Page Not Found!</h1>' } }

0 commit comments

Comments
 (0)