Skip to content

Commit af09819

Browse files
committed
Add router and part detail.
Added a router and component for part detail.
1 parent b8be08a commit af09819

File tree

8 files changed

+94
-20
lines changed

8 files changed

+94
-20
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
},
1010
"dependencies": {
1111
"core-js": "^2.6.5",
12-
"vue": "^2.6.10"
12+
"vue": "^2.6.10",
13+
"vue-router": "^3.0.6"
1314
},
1415
"devDependencies": {
1516
"@vue/cli-plugin-babel": "^3.8.0",

src/App.vue

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,25 @@
44
<nav>
55
<ul>
66
<li class="nav-item">
7-
<img class="logo" src="./assets/build-a-bot-logo.png">
8-
Build-a-Bot
7+
<router-link class="nav-link" :to="{ name: 'Home' }" exact>
8+
<img class="logo" src="./assets/build-a-bot-logo.png">Build-a-Bot
9+
</router-link>
10+
</li>
11+
<li class="nav-item">
12+
<router-link class="nav-link" :to="{ name: 'Build' }" exact>Build</router-link>
913
</li>
1014
</ul>
1115
</nav>
1216
</header>
1317
<main>
14-
<RobotBuilder/>
18+
<router-view/>
1519
</main>
1620
</div>
1721
</template>
1822

1923
<script>
20-
// import HomePage from './home/HomePage.vue';
21-
import RobotBuilder from './build/RobotBuilder.vue';
22-
2324
export default {
24-
name: 'app',
25-
components: {
26-
RobotBuilder,
27-
},
25+
name: "app"
2826
};
2927
</script>
3028

@@ -38,7 +36,7 @@ body {
3836

3937
<style scoped>
4038
#app {
41-
font-family: 'Avenir', Helvetica, Arial, sans-serif;
39+
font-family: "Avenir", Helvetica, Arial, sans-serif;
4240
}
4341
main {
4442
margin: 0 auto;
@@ -66,4 +64,11 @@ ul {
6664
vertical-align: middle;
6765
height: 30px;
6866
}
67+
.nav-link {
68+
text-decoration: none;
69+
color: inherit;
70+
}
71+
.router-link-active {
72+
color: white;
73+
}
6974
</style>

src/build/PartSelector.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div class="part" :class="position">
3-
<img :src="selectedPart.src" title="arm">
3+
<img @click="showPartInfo()" :src="selectedPart.src" title="arm">
44
<button @click="selectPreviousPart()" class="prev-selector"></button>
55
<button @click="selectNextPart()" class="next-selector"></button>
66
<span class="sale" v-show="selectedPart.onSale">Sale!</span>
@@ -44,6 +44,12 @@ export default {
4444
this.$emit("partSelected", this.selectedPart);
4545
},
4646
methods: {
47+
showPartInfo() {
48+
this.$router.push({
49+
name: "Parts",
50+
params: { id: this.selectedPart.id, partType: this.selectedPart.type }
51+
});
52+
},
4753
selectNextPart() {
4854
this.selectedPartIndex = getNextValidIndex(
4955
this.selectedPartIndex,
@@ -86,6 +92,7 @@ export default {
8692
}
8793
.part img {
8894
width: 165px;
95+
cursor: pointer;
8996
}
9097
.top {
9198
border-bottom: none;

src/home/HomePage.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
<img class="robot" src="../assets/robot-home.png" aria-hidden="true">
55
</div>
66
<div class="getting-started">
7-
<a href="">Get started</a> building your first robot.
7+
<router-link to="/build">Get started</router-link>&nbsp;building your first robot.
88
</div>
99
</div>
1010
</template>
1111

1212
<script>
1313
export default {
14-
name: 'HomePage',
14+
name: "HomePage",
1515
props: {
16-
msg: String,
17-
},
16+
msg: String
17+
}
1818
};
1919
</script>
2020

src/main.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import Vue from 'vue';
2-
import App from './App.vue';
1+
import Vue from "vue";
2+
import App from "./App.vue";
3+
4+
import router from "./router";
35

46
Vue.config.productionTip = false;
57

68
new Vue({
79
render: h => h(App),
8-
}).$mount('#app');
10+
router
11+
}).$mount("#app");

src/parts/PartInfo.vue

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<template>
2+
<div>
3+
<h1>{{part.title}}</h1>
4+
<div>{{part.description}}</div>
5+
</div>
6+
</template>
7+
8+
<script>
9+
import parts from "../data/parts";
10+
11+
export default {
12+
name: "PartInfo",
13+
computed: {
14+
part() {
15+
const { partType, id } = this.$route.params;
16+
17+
return parts[partType].find(part => part.id === +id);
18+
}
19+
}
20+
};
21+
</script>
22+
23+
<style>
24+
</style>

src/router/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Vue from "vue";
2+
import Router from "vue-router";
3+
4+
import HomePage from "../home/HomePage.vue";
5+
import RobotBuilder from "../build/RobotBuilder.vue";
6+
import PartInfo from "../parts/PartInfo.vue";
7+
8+
Vue.use(Router);
9+
10+
export default new Router({
11+
mode: "history",
12+
routes: [
13+
{
14+
path: "/",
15+
name: "Home",
16+
component: HomePage
17+
},
18+
{
19+
path: "/build",
20+
name: "Build",
21+
component: RobotBuilder
22+
},
23+
{
24+
path: "/parts/:partType/:id",
25+
name: "Parts",
26+
component: PartInfo
27+
}
28+
]
29+
});

0 commit comments

Comments
 (0)