So I’m new to VueJs so excuse any mistakes I make here. I have a simple front end application, which is meant to be two pages long. There’s an index route and a game route. The game route takes a path variable name that is meant to be displayed on the screen.
I’ve added the route, imported the components but whenever I try accessing the URL it just displays the index page. Does anyone know what I’m doing wrong? Thanks!
Here is my index.js file
JavaScript
x
23
23
1
import Vue from 'vue'
2
import Router from 'vue-router'
3
import home from '@/components/home'//index
4
import game from '@/components/game'
5
6
7
Vue.use(Router)
8
9
export default new Router({
10
routes: [
11
{
12
path: '/',
13
name: 'home',
14
component: home
15
},
16
{
17
path:'/game/:name',
18
name:'game',
19
component:game
20
}
21
]
22
})
23
Here is my game.vue file (It’s incomplete, I just want to get it to load first):
JavaScript
1
53
53
1
<template>
2
<div class="container-fluid m=0 p=0">
3
<div id="theGame" class="full-page p-4">
4
<div class="row">
5
<h1>Welcome {{route.params.name}}</h1>
6
</div>
7
8
</div>
9
</div>
10
</template>
11
<script>
12
const choices = ['Rock','Paper','Scissors']
13
export default {
14
data(){
15
return {
16
name:'game',
17
yourChoice: null,
18
aiChoice:null,
19
yourScore:0,
20
aiScore:0,
21
}
22
},
23
methods:{
24
startPlay : function(choice){
25
this.yourChoice=choice;
26
this.aiChoice = choices[Math.floor(Math.random()*choices.length)];
27
this.gamePlay();
28
},
29
gamePlay: function(){
30
if(this.yourChoice=='Rock' && this.aiChoice=='Scissors'){
31
this.yourScore++;
32
}
33
else if(this.yourChoice=='Paper' && this.aiChoice=='Rock'){
34
this.yourScore++;
35
}
36
else if(this.yourChoice=='Scissors' && this.aiChoice=='Paper'){
37
this.yourScore++;
38
}
39
else if(this.yourChoice==this.aiChoice){
40
console.log("Draw");
41
}
42
else{
43
this.aiScore++;
44
}
45
}
46
}
47
48
}
49
</script>
50
<style scoped>
51
52
</style>
53
Advertisement
Answer
You’re using hash mode by default that allows to access the routes prefixed by #
sign :
JavaScript
1
2
1
localhost:8080/#/game/bob
2
if you want to access it like localhost:8080/game/bob
you should add history mode to the router definition:
JavaScript
1
16
16
1
export default new Router({
2
mode: 'history',
3
routes: [
4
{
5
path: '/',
6
name: 'home',
7
component: home
8
},
9
{
10
path:'/game/:name',
11
name:'game',
12
component:game
13
}
14
]
15
})
16