Skip to content

Commit 87c576d

Browse files
committed
Client side routing
1 parent 7f51a2b commit 87c576d

File tree

9 files changed

+93
-19
lines changed

9 files changed

+93
-19
lines changed

ClientApp/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import Vue from 'vue';
22
import App from './components/App.vue';
33
import store from './vuex/store.js';
4+
import router from './router';
5+
46
const app = new Vue({
7+
router,
58
store,
69
...App //... is spread operator if App is Array; is rest(remaining) properties if App is Object
710
});

ClientApp/components/App.vue

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
2-
<script>
3-
import { mapGetters, mapActions } from 'vuex';
4-
import Message from './Message.vue';
5-
export default {
6-
components: { Message },
7-
computed: mapGetters(['messages', 'lastFetchedMessageDate']),
8-
methods: mapActions(['fetchMessages']),
9-
created () {
10-
return this.$store.dispatch('fetchInitialMessages')
11-
}
12-
}
13-
</script>
14-
151
<template>
162
<div>
173
<h1>Hello from Vue#!!!#</h1>
18-
<Message v-for="(msg, index) in messages" :message="msg" :key="index" />
19-
<button @click="fetchMessages(lastFetchedMessageDate)">Fetch a message</button>
4+
<router-link to="/">Dashboard</router-link>
5+
<router-link to="/messages">Message</router-link>
6+
7+
<router-view></router-view>
208
</div>
219
</template>

ClientApp/components/Dashboard.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<h1>Dashboard</h1>
3+
</template>

ClientApp/components/Messages.vue

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<template>
2+
<div>
3+
<Message v-for="(msg, index) in messages" :message="msg" :key="index" />
4+
<button @click="fetchMessages(lastFetchedMessageDate)">Fetch a message</button>
5+
</div>
6+
</template>
7+
8+
<script>
9+
import {mapGetters, mapActions} from 'vuex';
10+
import Message from './Message.vue';
11+
export default {
12+
components: { Message },
13+
computed: mapGetters(['messages', 'lastFetchedMessageDate']),
14+
methods: mapActions(['fetchMessages']),
15+
created(){
16+
return this.$store.dispatch('fetchInitialMessages');
17+
}
18+
}
19+
</script>

ClientApp/router/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Vue from 'vue';
2+
import VueRouter from 'vue-router';
3+
4+
import Dashboard from '../components/Dashboard.vue';
5+
import Messages from '../components/Messages.vue';
6+
7+
Vue.use(VueRouter);
8+
9+
const router = new VueRouter({
10+
mode: 'history',
11+
routes: [
12+
{ path: '/', component: Dashboard },
13+
{ path: '/messages', component: Messages }
14+
]
15+
});
16+
17+
export default router;

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ This repository is a clone from Stu Ratcliffe [Server rendering Vue.js applicati
33
Following his articile and trying to have some write up. (e.g. using latest packages in package.json)
44

55
## Packages used:
6-
- lodash <- similiar to numpy in Python
7-
- axios <- Promise based HTTP client for the browser and Node.js
6+
- lodash <- similiar to numpy in Python, utilies library for manipulating array/object.
7+
- axios <- Promise based HTTP client for the browser and Node.js, think of $.ajax() if you come from jQuery world
88
- vuex <- Vue variant of Flux implementation, just like Redux in React
99
### Working with data:
1010
- .Net part
@@ -14,4 +14,16 @@ Following his articile and trying to have some write up. (e.g. using latest pack
1414
- Vue part
1515
0. (create ClientApp/vuex folder)
1616
1. ClientApp/vuex/action.js
17-
2. ClientApp/vuex/store.js
17+
2. ClientApp/vuex/store.js
18+
### Client side routing:
19+
0. Move to ClientApp/components
20+
1. Dashboard.vue
21+
2. Messages.vue
22+
3. Modify App.vue
23+
4. create ClientApp/router folder
24+
5. Move to ClientApp/router
25+
6. index.js
26+
7. Move back to ClientApp folder
27+
8. Modify app.js
28+
9. Move back to .
29+
10. Modify Startup.cs

Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
4646

4747
app.UseMvc(routes =>
4848
{
49+
routes.MapSpaFallbackRoute("spa-fallback", new { controller = "Home", action = "Index"});
4950
routes.MapRoute(
5051
name: "default",
5152
template: "{controller=Home}/{action=Index}/{id?}");
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "vdn",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Ferry To",
10+
"license": "MIT",
11+
"private": true,
12+
"dependencies": {
13+
"vue": "^2.5.8",
14+
"vuex": "^3.0.1",
15+
"lodash": "^4.17.4",
16+
"axios": "^0.17.1"
17+
},
18+
"devDependencies": {
19+
"babel-core": "^6.26.0",
20+
"babel-loader": "^7.1.2",
21+
"babel-preset-es2015": "^6.24.1",
22+
"babel-preset-stage-2": "^6.24.1",
23+
"vue-loader": "^13.5.0",
24+
"vue-template-compiler": "^2.5.8",
25+
"css-loader": "^0.28.7",
26+
"aspnet-webpack": "^2.0.1",
27+
"webpack": "^3.8.1",
28+
"webpack-hot-middleware": "^2.20.0"
29+
}
30+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"vue": "^2.5.8",
1414
"vuex": "^3.0.1",
15+
"vue-router": "^3.0.1",
1516
"lodash": "^4.17.4",
1617
"axios": "^0.17.1"
1718
},

0 commit comments

Comments
 (0)