Skip to content

Commit 9faee64

Browse files
committed
Add vue router and routes
1 parent 73991b4 commit 9faee64

File tree

7 files changed

+124
-32
lines changed

7 files changed

+124
-32
lines changed

package-lock.json

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

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
"postcss-import": "^14.0.2",
1818
"postcss-preset-env": "^6.7.0",
1919
"tailwindcss": "npm:@tailwindcss/postcss7-compat@^2.0.2",
20-
"vue": "^3.0.0"
20+
"vue": "^3.0.0",
21+
"vue-router": "^4.0.0-0"
2122
},
2223
"devDependencies": {
2324
"@vue/cli-plugin-babel": "~4.5.0",
2425
"@vue/cli-plugin-eslint": "~4.5.0",
26+
"@vue/cli-plugin-router": "~4.5.0",
2527
"@vue/cli-service": "~4.5.0",
2628
"@vue/compiler-sfc": "^3.0.0",
2729
"babel-eslint": "^10.1.0",

src/App.vue

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
<template>
2-
<Home msg="Welcome to Your Vue.js App"/>
2+
<div id="nav">
3+
<router-link to="/">Home</router-link> |
4+
<router-link to="/about">About</router-link>
5+
</div>
6+
<router-view/>
37
</template>
48

5-
<script>
6-
import Home from './components/Home.vue'
7-
8-
export default {
9-
name: 'App',
10-
components: {
11-
Home
12-
}
13-
}
14-
</script>
15-
169
<style>
1710
#app {
1811
font-family: Avenir, Helvetica, Arial, sans-serif;
1912
-webkit-font-smoothing: antialiased;
2013
-moz-osx-font-smoothing: grayscale;
2114
text-align: center;
2215
color: #2c3e50;
23-
margin-top: 60px;
16+
}
17+
18+
#nav {
19+
padding: 30px;
20+
}
21+
22+
#nav a {
23+
font-weight: bold;
24+
color: #2c3e50;
25+
}
26+
27+
#nav a.router-link-exact-active {
28+
color: #42b983;
2429
}
2530
</style>

src/main.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
import { createApp } from 'vue'
2+
import { createWebHistory, createRouter } from 'vue-router'
23
import App from './App.vue'
34
import './assets/css/tailwind.css'
5+
import Home from './components/Home'
6+
import About from './components/About'
7+
import router from './router'
48

59
const feather = require('feather-icons')
610
feather.replace()
711

8-
createApp(App).mount('#app')
12+
const routes = [
13+
{ path: '/', component: Home },
14+
{ path: '/about', component: About },
15+
];
16+
17+
const router = createRouter({
18+
history: createWebHistory(),
19+
routes
20+
});
21+
22+
createApp(App).use(router).use(router).mount('#app')

src/router/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createRouter, createWebHistory } from 'vue-router'
2+
import Home from '../views/Home.vue'
3+
4+
const routes = [
5+
{
6+
path: '/',
7+
name: 'Home',
8+
component: Home
9+
},
10+
{
11+
path: '/about',
12+
name: 'About',
13+
// route level code-splitting
14+
// this generates a separate chunk (about.[hash].js) for this route
15+
// which is lazy-loaded when the route is visited.
16+
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
17+
}
18+
]
19+
20+
const router = createRouter({
21+
history: createWebHistory(process.env.BASE_URL),
22+
routes
23+
})
24+
25+
export default router

src/views/About.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<div class="about">
3+
<h1>This is an about page</h1>
4+
</div>
5+
</template>

src/views/Home.vue

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template>
2+
<div class="home">
3+
<img alt="Vue logo" src="../assets/logo.png">
4+
<HelloWorld msg="Welcome to Your Vue.js App"/>
5+
</div>
6+
</template>
7+
8+
<script>
9+
// @ is an alias to /src
10+
import HelloWorld from '@/components/HelloWorld.vue'
11+
12+
export default {
13+
name: 'Home',
14+
components: {
15+
HelloWorld
16+
}
17+
}
18+
</script>

0 commit comments

Comments
 (0)