Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
node-sample
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
周韬
node-sample
Commits
3af77f29
Commit
3af77f29
authored
Aug 04, 2016
by
Evan You
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
introduce the store
parent
6e2a998d
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
67 additions
and
19 deletions
+67
-19
server.js
server.js
+2
-0
App.vue
src/App.vue
+10
-1
app.js
src/app.js
+4
-12
index.js
src/router/index.js
+15
-0
server-entry.js
src/server-entry.js
+7
-2
index.js
src/store/index.js
+26
-0
webpack.client.config.js
webpack.client.config.js
+2
-3
webpack.server.config.js
webpack.server.config.js
+1
-1
No files found.
server.js
View file @
3af77f29
...
...
@@ -61,6 +61,7 @@ if (process.env.NODE_ENV !== 'production') {
app
.
use
(
favicon
(
path
.
resolve
(
__dirname
,
'src/assets/logo.png'
)))
app
.
get
(
'*'
,
(
req
,
res
)
=>
{
var
s
=
Date
.
now
()
const
context
=
{
url
:
req
.
url
}
const
renderStream
=
renderer
.
renderToStream
(
context
)
let
firstChunk
=
true
...
...
@@ -88,6 +89,7 @@ app.get('*', (req, res) => {
renderStream
.
on
(
'end'
,
()
=>
{
res
.
end
(
`<script src="/dist/client-bundle.js"></script></body></html>`
)
console
.
log
(
'whole request: '
+
(
Date
.
now
()
-
s
))
})
renderStream
.
on
(
'error'
,
err
=>
{
...
...
src/App.vue
View file @
3af77f29
...
...
@@ -2,11 +2,14 @@
<div
id=
"app"
>
<img
src=
"./assets/logo.png"
>
<h1>
{{
msg
}}
</h1>
<p>
Current URL is:
{{
$store
.
state
.
url
}}
(from the store)
</p>
<ul>
<li><router-link
to=
"/"
>
News
</router-link></li>
<li><router-link
to=
"/about"
>
About
</router-link></li>
</ul>
<router-view></router-view>
<transition
name=
"view"
mode=
"out-in"
>
<router-view
class=
"view"
></router-view>
</transition>
</div>
</
template
>
...
...
@@ -25,4 +28,10 @@ export default {
body
{
font-family
:
Helvetica
,
sans-serif
;
}
.view
{
transition
:
all
.35s
ease
;
}
.view-enter
,
.view-leave-active
{
opacity
:
0
;
}
</
style
>
src/app.js
View file @
3af77f29
import
Vue
from
'vue'
import
Router
from
'vue-router'
import
App
from
'./App.vue'
import
News
from
'./views/News.vue
'
import
About
from
'./views/About.vu
e'
import
router
from
'./router
'
import
store
from
'./stor
e'
Vue
.
use
(
Router
)
export
const
router
=
new
Router
({
mode
:
'history'
,
routes
:
[
{
path
:
'/'
,
component
:
News
},
{
path
:
'/about'
,
component
:
About
}
]
})
export
{
router
,
store
}
export
const
app
=
new
Vue
({
router
,
store
,
render
:
h
=>
h
(
App
)
})
src/router/index.js
0 → 100644
View file @
3af77f29
import
Vue
from
'vue'
import
Router
from
'vue-router'
Vue
.
use
(
Router
)
import
News
from
'../views/News.vue'
import
About
from
'../views/About.vue'
export
default
new
Router
({
mode
:
'history'
,
routes
:
[
{
path
:
'/'
,
component
:
News
},
{
path
:
'/about'
,
component
:
About
}
]
})
src/server-entry.js
View file @
3af77f29
import
{
app
,
router
}
from
'./app'
import
{
app
,
router
,
store
}
from
'./app'
export
default
context
=>
{
// set router's initial location
router
.
setInitialLocation
(
context
.
url
)
return
app
// resolve store state
return
store
.
dispatch
(
'setURL'
,
context
.
url
).
then
(()
=>
{
context
.
initialState
=
store
.
state
return
app
})
}
src/store/index.js
0 → 100644
View file @
3af77f29
import
Vue
from
'vue'
import
Vuex
from
'vuex'
Vue
.
use
(
Vuex
)
// pre-fetched state injected by SSR
const
serverState
=
typeof
window
!==
'undefined'
&&
window
.
__INITIAL_STATE__
// default state
const
defaultState
=
{
url
:
'/'
}
export
default
new
Vuex
.
Store
({
state
:
serverState
||
defaultState
,
mutations
:
{
setURL
:
(
state
,
url
)
=>
state
.
url
=
url
},
actions
:
{
// just simulating an async action here
setURL
:
({
commit
},
url
)
=>
new
Promise
(
resolve
=>
{
commit
(
'setURL'
,
url
)
setTimeout
(
resolve
,
0
)
})
}
})
webpack.client.config.js
View file @
3af77f29
...
...
@@ -3,7 +3,7 @@ const webpack = require('webpack')
const
isProd
=
process
.
env
.
NODE_ENV
===
'production'
module
.
exports
=
{
devtool
:
'#
eval-
source-map'
,
devtool
:
'#source-map'
,
entry
:
'./src/client-entry.js'
,
output
:
{
path
:
path
.
resolve
(
__dirname
,
'./dist'
),
...
...
@@ -46,8 +46,7 @@ module.exports = {
]
}
if
(
process
.
env
.
NODE_ENV
===
'production'
)
{
module
.
exports
.
devtool
=
'#source-map'
if
(
process
.
env
.
NODE_ENV
===
'production'
&&
process
.
env
.
VUE_ENV
!==
'server'
)
{
module
.
exports
.
plugins
=
module
.
exports
.
plugins
.
concat
([
new
webpack
.
optimize
.
UglifyJsPlugin
({
compress
:
{
...
...
webpack.server.config.js
View file @
3af77f29
...
...
@@ -12,7 +12,7 @@ module.exports = merge(webpackConfig, {
plugins
:
[
new
webpack
.
DefinePlugin
({
'process.env'
:
{
NODE_ENV
:
'"production"'
,
NODE_ENV
:
JSON
.
stringify
(
process
.
env
.
NODE_ENV
||
'development'
)
,
VUE_ENV
:
'"server"'
}
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment