经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
Vue.js?状态管理及?SSR解析
来源:jb51  时间:2022/9/15 9:08:57  对本文有异议

前端状态管理出现的意义及解决的问题

随着前端应用的逐步复杂,我们的组件中需要使用越来越多的状态。有的时候我们需要使用子组件将状态传递给父组件就会比较复杂,数据的向上传递过程我们可能会使用回调函数或是数据绑定的形式去处理,就会让代码晦涩难懂。

我们需要一种方式,能够让数据在所有组件中共享,同时能以简单的方式进行传递,这种组织数据的方式就是状态管理。我们很自然的就想到,把数据放到所有需要使用的组件的公共祖先上,在使用时自上而下传递即可。

在 vue.js 中,我们主要说的状态管理库就是 vuex,当然,只要你能实现有条理的组织数据,那么它都可以认为是一种状态管理库。

事实上,我们可以简单的这样理解【状态管理】这个词,vuex 实际上做的事情就是:

  • 在顶层实现一个数据管理的仓库 store,将所有组件间需要共享的数据放置于此;
  • 同时组件也可以对这个 store 内的数据进行更新,同时更新完之后响应式更新所有使用此数据组件的视图;

Vuex 源码解读

Vuex 公共方法

  • 路径:src\util.js
  1. export function find(list, f) {
  2. return list.filter(f)[0];
  3. }
  4.  
  5. export function deepCopy(obj, cache = []) {
  6. if (obj === null || typeof obj !== 'object') {
  7. return obj;
  8. }
  9.  
  10. const hit = find(cache, c => c.original === obj);
  11. if (hit) {
  12. return hit.copy;
  13. }
  14.  
  15. const copy = Array.isArray(obj) ? [] : {};
  16. cache.push({
  17. original: obj,
  18. copy,
  19. });
  20.  
  21. Object.keys(obj).forEach(key => {
  22. copy[key] = deepCopy(obj[key], cache);
  23. });
  24.  
  25. return copy;
  26. }
  27.  
  28. export function forEachValue(obj, fn) {
  29. Object.keys(obj).forEach(key => fn(obj[key], key));
  30. }
  31.  
  32. export function isObject(obj) {
  33. return obj !== null && typeof obj === 'object';
  34. }
  35.  
  36. export function isPromise(val) {
  37. return val && typeof val.then === 'function';
  38. }
  39.  
  40. export function assert(condition, msg) {
  41. if (!condition) throw new Error(`[vuex] ${msg}`);
  42. }
  43.  
  44. export function partial(fn, arg) {
  45. return function () {
  46. return fn(arg);
  47. };
  48. }

Vuex 介绍及深入使用

在说 vuex 之前,我们必须说一说 flux 架构,flux 架构可谓是状态管理的鼻祖。

flux 架构最早由 facebook 推出,主要是为了处理当时他们的 react 框架下状态管理的问题,但在当时来讲,整个设计比较复杂,后来人们简化了其中的一些理念,但是保留了核心思想,继而依据框架实现了很多不同的状态管理库,例如 reduxvuex 等等。其中 redux 大多数被用在了 react 项目中,而 vuex 就是在 vue 框架中实现的这么一个 flux 架构的状态管理库。

**flux 架构约定,存放数据的地方称为 storestore 内部的 state 是数据本身,我们必须通过 action 才能修改 store 里的 state。**这里的 action 指的意思是 行为,在大部分实现里面是一个函数,通过调用函数来更改 store 内部的 state

vuex 中,我们可以通过 mutation 来【同步】的改变 state,这时候就可以在组件中通过 commit 进行调用更改 state

同样的,我们也可以通过 action 来【异步】更改 state,不过在 action 中,我们还是需要调用 mutation

Vuex 使用(官网)

网址链接:vuex.vuejs.org/zh/guide/st…

1、基本框架

  1. import Vue from 'vue';
  2. import Vuex from 'vuex';
  3.  
  4. Vue.use(Vuex);
  5.  
  6. export default new Vuex.Store({
  7. state: {},
  8. mutations: {},
  9. actions: {},
  10. modules: {},
  11. });

2、基本使用

  • ./store/index.js
  1. import Vue from 'vue';
  2. import Vuex from 'vuex';
  3.  
  4. Vue.use(Vuex);
  5.  
  6. export default new Vuex.Store({
  7. state: {
  8. count: 0,
  9. },
  10. mutations: {
  11. increment(state, payload = 1) {
  12. state.count += payload;
  13. },
  14. },
  15. actions: {},
  16. modules: {},
  17. });
  • ./Home.vue
  1. <template>
  2. <div class="home">
  3. <div>count: {{ count }}</div>
  4. <button @click="increment">增加</button>
  5. <button @class="decrement">减少</button>
  6. </div>
  7. </template>
  8.  
  9. <script>
  10. export default {
  11. name: 'Home',
  12. computed: {
  13. count() {
  14. return this.$store.state.count;
  15. },
  16. },
  17. methods: {
  18. increment() {
  19. // 使用 commit 派发 mutation 事件
  20. // this.$store.commit("increment");
  21. // 可以传递参数
  22. this.$store.commit('increment', 2);
  23. },
  24. decrement() {},
  25. },
  26. };
  27. </script>

3、State

可以使用计算属性获取 state 中的数据:

  1. computed: {
  2. count () {
  3. return this.$store.state.count
  4. }
  5. }

3.1 mapState 辅助函数

当一个组件需要获取多个状态的时候,将这些状态都声明为计算属性会有些重复和冗余。为了解决这个问题,我们可以使用 mapState 辅助函数帮助我们生成计算属性。

  1. import { mapState } from "vuex";
  2.  
  3. computed: {
  4. ...mapState(["num"])
  5. }

4、Getter

Vuex 允许我们在 store 中定义 getter(可以认为是 store 的计算属性)。就像计算属性一样,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算。

Getter 接受 state 作为其第一个参数:

  1. const store = new Vuex.Store({
  2. state: {
  3. todos: [
  4. { id: 1, text: 'foo', done: true },
  5. { id: 2, text: 'bar', done: false },
  6. ],
  7. },
  8. getters: {
  9. doneTodos: state => {
  10. return state.todos.filter(todo => todo.done);
  11. },
  12. },
  13. });

4.1 通过属性访问

Getter 会暴露为 store.getters 对象,你可以以属性的形式访问这些值:

  1. store.getters.doneTodos; // -> [{ id: 1, text: '...', done: true }]

Getter 也可以接受其他 getter 作为第二个参数:

  1. getters: {
  2. // ...
  3. doneTodosCount: (state, getters) => {
  4. return getters.doneTodos.length;
  5. };
  6. }

4.2 通过方法访问

你也可以通过让 getter 返回一个函数,来实现给 getter 传参。

  1. getters: {
  2. // ...
  3. getTodoById: state => id => {
  4. return state.todos.find(todo => todo.id === id);
  5. };
  6. }
  1. store.getters.getTodoById(2); // -> { id: 2, text: '...', done: false }

【注意】:getter 在通过方法访问时,每次都会去进行调用,而不会缓存结果

4.3 mapGetters 辅助函数

mapGetters 辅助函数仅仅是将 store 中的 getter 映射到局部计算属性:

  1. import { mapGetters } from 'vuex';
  2.  
  3. export default {
  4. computed: {
  5. // 使用对象展开运算符将 getter 混入 computed 对象中
  6. ...mapGetters(['doneTodosCount', 'anotherGetter']),
  7. },
  8. };

如果你想将一个 getter 属性另取一个名字,使用对象形式:

  1. ...mapGetters({
  2. // 把 this.doneCount 映射为 this.$store.getters.doneTodosCount
  3. doneCount: 'doneTodosCount'
  4. })

5、Mutation

更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。Vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数:

  1. const store = new Vuex.Store({
  2. state: {
  3. count: 1,
  4. },
  5. mutations: {
  6. increment(state) {
  7. // 变更状态
  8. state.count++;
  9. },
  10. },
  11. });

你不能直接调用一个 mutation handler。这个选项更像是事件注册:“当触发一个类型为 increment 的 mutation 时,调用此函数。”要唤醒一个 mutation handler,你需要以相应的 type 调用 store.commit 方法:

  1. store.commit('increment');

5.1 提交载荷(Payload)

你可以向 store.commit 传入额外的参数,即 mutation 的 载荷(payload

  1. mutations: {
  2. increment (state, n) {
  3. state.count += n
  4. }
  5. }
  6.  
  7. // 使用方法
  8. store.commit('increment', 10)

在大多数情况下,载荷应该是一个对象,这样可以包含多个字段并且记录的 mutation 会更易读:

  1. mutations: {
  2. increment (state, payload) {
  3. state.count += payload.amount
  4. }
  5. }
  6.  
  7. // 使用方法
  8. store.commit('increment', {
  9. amount: 10
  10. })

对象风格的提交方式:

提交 mutation 的另一种方式是直接使用包含 type 属性的对象:

  1. store.commit({
  2. type: 'increment',
  3. amount: 10,
  4. });

当使用对象风格的提交方式,整个对象都作为载荷传给 mutation 函数,因此 handler 保持不变:

  1. mutations: {
  2. increment (state, payload) {
  3. state.count += payload.amount
  4. }
  5. }

5.2 使用常量替代 Mutation 事件类型

使用常量替代 mutation 事件类型在各种 Flux 实现中是很常见的模式。这样可以使 linter 之类的工具发挥作用,同时把这些常量放在单独的文件中可以让你的代码合作者对整个 app 包含的 mutation 一目了然:

  1. // mutation-types.js
  2. export const SOME_MUTATION = 'SOME_MUTATION';
  1. // store.js
  2. import Vuex from 'vuex'
  3. import { SOME_MUTATION } from './mutation-types'
  4.  
  5. const store = new Vuex.Store({
  6. state: { ... },
  7. mutations: {
  8. // 我们可以使用 ES2015 风格的计算属性命名功能来使用一个常量作为函数名
  9. [SOME_MUTATION] (state) {
  10. // mutate state
  11. }
  12. }
  13. })

5.3 Mutation 必须是同步函数

一条重要的原则就是要记住 mutation 必须是同步函数。为什么?请参考下面的例子:

  1. mutations: {
  2. someMutation (state) {
  3. api.callAsyncMethod(() => {
  4. state.count++
  5. })
  6. }
  7. }

现在想象,我们正在 debug 一个 app 并且观察 devtool 中的 mutation 日志。每一条 mutation 被记录,devtools 都需要捕捉到前一状态和后一状态的快照。然而,在上面的例子中 mutation 中的异步函数中的回调让这不可能完成:因为当 mutation 触发的时候,回调函数还没有被调用,devtools 不知道什么时候回调函数实际上被调用 —— 实质上任何在回调函数中进行的状态的改变都是不可追踪的。

5.4 在组件中提交 Mutation

你可以在组件中使用 this.$store.commit('xxx') 提交 mutation,或者使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit 调用(需要在根节点注入 store)。

  1. import { mapMutations } from 'vuex';
  2.  
  3. export default {
  4. // ...
  5. methods: {
  6. ...mapMutations([
  7. 'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`
  8.  
  9. // `mapMutations` 也支持载荷:
  10. 'incrementBy', // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`
  11. ]),
  12. ...mapMutations({
  13. add: 'increment', // 将 `this.add()` 映射为 `this.$store.commit('increment')`
  14. }),
  15. },
  16. };

6、Action

Action 类似于 mutation,不同在于:

  • Action 提交的是 mutation,而不是直接变更状态。
  • Action 可以包含任意异步操作。

让我们来注册一个简单的 action

  1. const store = new Vuex.Store({
  2. state: {
  3. count: 0,
  4. },
  5. mutations: {
  6. increment(state) {
  7. state.count++;
  8. },
  9. },
  10. actions: {
  11. increment(context) {
  12. context.commit('increment');
  13. },
  14. },
  15. });

Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象,因此你可以调用 context.commit 提交一个 mutation,或者通过 context.state 和 context.getters 来获取 state 和 getters。当我们在之后介绍到 Modules 时,你就知道 context 对象为什么不是 store 实例本身了。

实践中,我们会经常用到 ES2015 的参数解构来简化代码(特别是我们需要调用 commit 很多次的时候):

  1. actions: {
  2. increment ({ commit, state, getters }) {
  3. commit('increment')
  4. }
  5. }

7、分发 Action

Action 通过 store.dispatch 方法触发:

  1. store.dispatch('increment');

乍一眼看上去感觉多此一举,我们直接分发 mutation 岂不更方便?实际上并非如此,还记得 mutation 必须同步执行这个限制么?Action 就不受约束!我们可以在 action 内部执行异步操作:

  1. actions: {
  2. incrementAsync ({ commit }) {
  3. setTimeout(() => {
  4. commit('increment')
  5. }, 1000)
  6. }
  7. }

Actions 支持同样的载荷方式和对象方式进行分发:

  1. // 以载荷形式分发
  2. store.dispatch('incrementAsync', {
  3. amount: 10,
  4. });
  5.  
  6. // 以对象形式分发
  7. store.dispatch({
  8. type: 'incrementAsync',
  9. amount: 10,
  10. });

7.1 在组件中分发 Action

你在组件中使用 this.$store.dispatch('xxx') 分发 action,或者使用 mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用(需要先在根节点注入 store):

  1. import { mapActions } from 'vuex';
  2.  
  3. export default {
  4. // ...
  5. methods: {
  6. ...mapActions([
  7. 'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`
  8.  
  9. // `mapActions` 也支持载荷:
  10. 'incrementBy', // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`
  11. ]),
  12. ...mapActions({
  13. add: 'increment', // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
  14. }),
  15. },
  16. };

7.2 组合 Action

Action 通常是 异步 的,那么如何知道 action 什么时候结束呢?更重要的是,我们如何才能组合多个 action,以处理更加复杂的异步流程?

首先,你需要明白 store.dispatch 可以处理被触发的 action 的处理函数返回的 Promise,并且 store.dispatch 仍旧返回 Promise

  1. actions: {
  2. actionA ({ commit }) {
  3. return new Promise((resolve, reject) => {
  4. setTimeout(() => {
  5. commit('someMutation')
  6. resolve()
  7. }, 1000)
  8. })
  9. }
  10. }

现在你可以:

  1. store.dispatch('actionA').then(() => {
  2. // ...
  3. });

在另外一个 action 中也可以:

  1. actions: {
  2. // ...
  3. actionB ({ dispatch, commit }) {
  4. return dispatch('actionA').then(() => {
  5. commit('someOtherMutation')
  6. })
  7. }
  8. }

最后,如果我们利用 async / await,我们可以如下组合 action

  1. // 假设 getData() 和 getOtherData() 返回的是 Promise
  2. actions: {
  3. async actionA ({ commit }) {
  4. commit('gotData', await getData())
  5. },
  6. async actionB ({ dispatch, commit }) {
  7. await dispatch('actionA') // 等待 actionA 完成
  8. commit('gotOtherData', await getOtherData())
  9. }
  10. }

一个 store.dispatch 在不同模块中可以触发多个 action 函数。在这种情况下,只有当所有触发函数完成后,返回的 Promise 才会执行。

8、严格模式

开启严格模式,仅需在创建 store 的时候传入 strict: true

  1. const store = new Vuex.Store({
  2. // ...
  3. strict: true,
  4. });

在严格模式下,无论何时发生了状态变更且不是由 mutation 函数引起的,将会抛出错误(但是数据还是会改变)。这能保证所有的状态变更都能被调试工具跟踪到。

8.1 开发环境与发布环境

不要在发布环境下启用严格模式! 严格模式会深度监测状态树来检测不合规的状态变更 —— 请确保在发布环境下关闭严格模式,以避免性能损失。

类似于插件,我们可以让构建工具来处理这种情况:

  1. const store = new Vuex.Store({
  2. // ...
  3. strict: process.env.NODE_ENV !== 'production',
  4. });

vue.js 服务端渲染介绍

大多数我们使用的 UI 框架如 vue 和 react,都是在客户端进行渲染,也就是说每个用户在加载进来我们所有的 html 文件和 js 文件之后,才开始渲染页面的内容。

但是这样做会有两个问题,一个是如果用户网络速度比较慢,如果我们渲染的内容比较多的话,就会产生一个延迟,造成不好的用户体验。另一个是某些爬虫,例如百度的搜索收录的爬虫,在爬取你的页面时,获取不到你的页面的真实内容,导致站点 SEO 权重变低。

所以很多需要 SEO 的页面,都需要在服务端提前渲染好 html 的内容,在用户访问时先返回给用户内容,这杨对用户和爬虫都非常友好。

我们可以通过直接在页面上右击查看网页源代码,来查看一个页面是否有服务端渲染。

1、客户端渲染和服务端渲染

客户端渲染

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>客户端渲染</title>
  7. </head>
  8. <body>
  9. <script>
  10. document.body.innerHTML = '<div>你好</div>';
  11. </script>
  12. </body>
  13. </html>

在 Network 的中 Preview 中无数据,在 Response 中的没有 DOM 标签:

查看网页源代码:

g

2、服务端渲染

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>服务端渲染</title>
  8. </head>
  9. <body>
  10. <div>你好</div>
  11. </body>
  12. </html>

在 Network 的中 Preview 中有数据,在 Response 中的有 DOM 标签:

查看网页源代码:

客户端路由

在控制台中可以看到,切换路由的时候并没有发起 ajax 请求。

3、服务端渲染实例

vue.js 的服务端渲染非常简单,我们只需要在 node.js 中通过 vue-server-renderer 模块,调用对应服务端渲染的渲染器对组件渲染即可,他就会生成组件对应的 html 内容。渲染成功的 html 标签,我们可以直接返回到客户端作为初始请求 html 的返回值。

  • 安装依赖
  1. yarn add express vue vue-server-renderer
  • ./index.js
  1. const Vue = require('vue');
  2. const createRenderer = require('vue-server-renderer').createRenderer;
  3.  
  4. const vm = new Vue({
  5. data() {
  6. return {
  7. count: 100,
  8. };
  9. },
  10. template: `<div>{{ count }}</div>`,
  11. });
  12.  
  13. const renderer = createRenderer();
  14.  
  15. renderer.renderToString(vm, (err, html) => {
  16. console.log('html ==========', html); // <div data-server-rendered="true">100</div>
  17. });
  • ./index.js
  1. const Vue = require('vue');
  2. const createRenderer = require('vue-server-renderer').createRenderer;
  3. const express = require('express');
  4. const fs = require('fs');
  5. const path = require('path');
  6.  
  7. const app = express();
  8.  
  9. // ! 服务端路由
  10. app.get('*', function (req, res) {
  11. const vm = new Vue({
  12. data() {
  13. return {
  14. url: `服务端路由 ${req.url}`,
  15. count: 100,
  16. };
  17. },
  18. template: `<div>{{ url }} - {{ count }}</div>`,
  19. });
  20.  
  21. const renderer = createRenderer({
  22. // 设置模板
  23. template: fs.readFileSync(path.resolve(__dirname, './index.template.html'), 'utf-8'),
  24. });
  25.  
  26. renderer.renderToString(vm, (err, html) => {
  27. res.send(html);
  28. });
  29. });
  30.  
  31. const PORT = 8080;
  32. app.listen(PORT, () => {
  33. console.log(`服务器启动在 ${PORT} 端口`);
  34. });
  • ./index.template.html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>服务端渲染Demo</title>
  8. </head>
  9. <body>
  10. <h1>这里是模板</h1>
  11. <!--! 中间不能带空格 -->
  12. <!--vue-ssr-outlet-->
  13. </body>
  14. </html>
  • 执行 index.js 文件:node ./index.js

我们需要注意的一点是,在服务端渲染组件,我们使用不了 windowlocation 等浏览器环境中的对象,所以如果组件内部使用了这种内容会报错。

同时,在服务端渲染时我们要注意,组件的生命周期也会只执行 beforeCreate 和 created 这两个,所以在此声明周期里面不能使用 window,但是可以在其他声明周期比如 mounted 中使用。还有渲染的数据,对于服务端渲染的组件来说,我们不应该发请求获取组件数据,而是应该直接渲染时使用数据进行渲染。

路由也是如此,在 vue 客户端使用路由的时候,我们也需要在服务端对路由进行匹配,从而得知具体需要渲染的组件是哪个。

3、同构 - 客户端渲染和服务端渲染

参考文档(Vue 文档中 - Vue 服务端渲染):ssr.vuejs.org/zh/

  • ./webpack.config.js
  1. /*
  2. 客户端 webpack 配置
  3. */
  4. const path = require('path');
  5. const webpack = require('webpack');
  6.  
  7. module.exports = {
  8. entry: './src/entry-client.js',
  9. output: {
  10. path: path.resolve(__dirname, './dist'),
  11. publicPath: '/dist/',
  12. filename: 'build.js',
  13. },
  14. module: {
  15. rules: [
  16. {
  17. test: /\.vue$/,
  18. loader: 'vue-loader',
  19. },
  20. {
  21. test: /\.js$/,
  22. loader: 'babel-loader',
  23. exclude: /node_modules/,
  24. },
  25. ],
  26. },
  27. resolve: {
  28. extensions: ['*', '.js', '.vue'],
  29. },
  30. };
  • ./webpack.server.config.js
  1. /*
  2. 服务端 webpack 配置
  3. */
  4. const path = require('path');
  5. const webpack = require('webpack');
  6.  
  7. const merge = require('webpack-merge');
  8. const baseWebpackConfig = require('./webpack.config');
  9.  
  10. const config = merge(baseWebpackConfig, {
  11. target: 'node',
  12. entry: {
  13. app: './src/entry-server.js',
  14. },
  15. output: {
  16. path: __dirname,
  17. filename: 'server.bundle.js',
  18. libraryTarget: 'commonjs2',
  19. },
  20. });
  21.  
  22. console.log('config ============ ', config);
  23. module.exports = config;
  • ./package.json
  1. {
  2. "name": "06",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "webpack.config.js",
  6. "dependencies": {
  7. "babel-core": "^6.26.3",
  8. "babel-loader": "^7.1.5",
  9. "babel-preset-env": "^1.7.0",
  10. "babel-preset-stage-3": "^6.24.1",
  11. "express": "^4.17.1",
  12. "vue": "^2.6.11",
  13. "vue-router": "^3.3.2",
  14. "vue-server-renderer": "^2.6.11",
  15. "vuex": "^3.4.0",
  16. "webpack": "^3.12.0",
  17. "webpack-merge": "^4.2.2"
  18. },
  19. "devDependencies": {
  20. "vue-loader": "^13.7.3",
  21. "vue-template-compiler": "^2.6.11"
  22. },
  23. "scripts": {
  24. "build-server": "webpack --config webpack.server.config.js",
  25. "build-client": "webpack --config webpack.config.js"
  26. },
  27. "keywords": [],
  28. "author": "",
  29. "license": "ISC"
  30. }
  • ./src/App.vue
  1. <template>
  2. <div>
  3. <h1>this is App.vue</h1>
  4.  
  5. <router-link to="/">root</router-link>
  6. <router-link to="/about">about</router-link>
  7. <router-view></router-view>
  8. </div>
  9. </template>
  • ./src/Home.vue
  1. <template>
  2. <div>Home {{ $store.state.timestamp }}</div>
  3. </template>
  • ./src/About.vue
  1. <template>
  2. <div>about {{ $store.state.timestamp }}</div>
  3. </template>
  • ./index.js
  1. /*
  2. entry-client 和 entry-server 共同的文件
  3. */
  4. import Vue from 'vue';
  5. import Router from 'vue-router';
  6. import Vuex from 'vuex';
  7. import Home from './Home';
  8. import About from './About';
  9. import App from './App';
  10.  
  11. Vue.use(Router);
  12. Vue.use(Vuex);
  13.  
  14. export function createApp() {
  15. const store = new Vuex.Store({
  16. state: {
  17. timestamp: new Date().getTime(),
  18. },
  19. });
  20.  
  21. if (typeof window !== 'undefined' && window.store) {
  22. store.replaceState(window.store);
  23. }
  24.  
  25. const router = new Router({
  26. mode: 'history',
  27. routes: [
  28. { path: '/', component: Home },
  29. { path: '/about', component: About },
  30. ],
  31. });
  32.  
  33. const vm = new Vue({
  34. router,
  35. store,
  36. render: h => h(App),
  37. });
  38.  
  39. return { vm, router, store };
  40. }
  • ./src/entry-server.js 第一种
  1. /*
  2. 服务端渲染 - 入口
  3. */
  4. const express = require('express');
  5. const fs = require('fs');
  6. const path = require('path');
  7. const renderer = require('vue-server-renderer').createRenderer();
  8.  
  9. const { createApp } = require('./index');
  10.  
  11. const app = express();
  12.  
  13. app.use('/dist', express.static(path.join(__dirname, './dist')));
  14.  
  15. app.get('/build.js', function (req, res) {
  16. const pathUrl = path.resolve(process.cwd(), './dist/build.js');
  17. console.log(pathUrl);
  18. res.sendFile(pathUrl);
  19. });
  20.  
  21. app.get('*', function (req, res) {
  22. const url = req.url;
  23. const { vm, router } = createApp();
  24. router.push(url);
  25.  
  26. /*
  27. const matchedComponents: Array<Component> = router.getMatchedComponents(location?)
  28. 返回目标位置或是当前路由匹配的组件数组 (是数组的定义/构造类,不是实例)。通常在服务端渲染的数据预加载时使用。
  29. */
  30. const matchedComponent = router.getMatchedComponents();
  31. if (!matchedComponent) {
  32. // 404 处理
  33. } else {
  34. renderer.renderToString(vm, function (err, html) {
  35. res.send(html);
  36. });
  37. }
  38. });
  39.  
  40. const PORT = 8080;
  41. app.listen(PORT, () => {
  42. console.log(`服务器启动在 ${PORT} 端口`);
  43. });
  44.  
  45. /*
  46. 此时可以执行 yarn build-server 编译 entry-server 文件,生成 server.bundle.js
  47. 执行 node ./server.bundle.js 查看服务端路由的结果
  48. */
  • ./src/entry-server.js 第二种
  1. /*
  2. 服务端渲染 - 入口
  3. */
  4. const express = require('express');
  5. const fs = require('fs');
  6. const path = require('path');
  7. const renderer = require('vue-server-renderer').createRenderer({
  8. template: fs.readFileSync(path.resolve(process.cwd(), './index.template.html'), 'utf-8'),
  9. });
  10.  
  11. const { createApp } = require('./index');
  12.  
  13. const app = express();
  14.  
  15. app.use('/dist', express.static(path.join(__dirname, './dist')));
  16.  
  17. app.get('/build.js', function (req, res) {
  18. const pathUrl = path.resolve(process.cwd(), './dist/build.js');
  19. console.log(pathUrl);
  20. res.sendFile(pathUrl);
  21. });
  22.  
  23. app.get('*', function (req, res) {
  24. const url = req.url;
  25. const { vm, router, store } = createApp();
  26. router.push(url);
  27.  
  28. /*
  29. const matchedComponents: Array<Component> = router.getMatchedComponents(location?)
  30. 返回目标位置或是当前路由匹配的组件数组 (是数组的定义/构造类,不是实例)。通常在服务端渲染的数据预加载时使用。
  31. */
  32. const matchedComponent = router.getMatchedComponents();
  33. if (!matchedComponent) {
  34. // 404 处理
  35. } else {
  36. renderer.renderToString(vm, function (err, html) {
  37. res.send(html);
  38. });
  39. }
  40. });
  41.  
  42. const PORT = 8080;
  43. app.listen(PORT, () => {
  44. console.log(`服务器启动在 ${PORT} 端口`);
  45. });
  • ./src/entry-client.js
  1. /*
  2. 客户端渲染 - 入口
  3. */
  4. import { createApp } from './index';
  5.  
  6. const { vm } = createApp();
  7.  
  8. vm.$mount('#app');
  • ./index.template.html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <h1>这里是模板</h1>
  11. <!--vue-ssr-outlet-->
  12. </div>
  13. <script src="/build.js"></script>
  14. </body>
  15. </html>

执行 yarn build-client 编译客户端;

执行 yarn build-server 编译服务端;

执行 node ./server.bundle.js 启动服务器,打开浏览器输入网址 http://localhost:8080/

到此这篇关于Vue.js 状态管理及 SSR解析的文章就介绍到这了,更多相关Vue.js SSR内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号