V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
id4alex
V2EX  ›  问与答

[不懂就问]关于 nuxtjs ssr 的一个问题

  •  1
     
  •   id4alex · 2020-11-16 14:39:00 +08:00 · 231 次点击
    这是一个创建于 1262 天前的主题,其中的信息可能已经有所发展或是发生改变。

    目前正在学习 nuxtjs 前端开发,目前遇到一个场景, 如果客户端 cookie 中携带 token,那么页面在 server side 就把用户信息渲染好. 代码如下:

    store/index.js

    export const state = () => ({
        user: null,
        token: null
    });
    
    export const mutations = {
        saveUser(state, userValue) {
            console.log("这里执行 store.user.save = " + JSON.stringify(userValue));
            state.user = userValue;
        },
        saveToken(state, token) {
            console.log("这里执行 store.token.save = " + token);
            state.token = token;
        }
    };
    
    export const actions = {
        nuxtServerInit({ commit }, app) {
            const token = app.$cookies.get("token") && app.$cookies.get("token").indexOf("Bearer ") == 0 ? app.$cookies.get("token") : null;
            console.log('nuxtserver init 初始化 token = ', token)
            if (token) {
                commit("saveToken", app.$cookies.get("token"));
                this.$axios.post("/api/me").then(res => {
                    commit("saveUser", res.data.data);
                });
            }
        }
    };
    

    pages/index.vue

    <template>
      <div>{{user}}</div>
    </template>
    
    <script>
    export default {
      computed: {
        user() {
          console.log("这里执行 computed " + this.$store.state.token)
          return this.$store.state.user;
        },
      },
    }
    </script>
    
    <style>
    </style>
    
    

    后台输出

    nuxtserver init 初始化 token =  Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI0OWYxMjgwZTkxODRjZmMyIiwiZXhwIjoxNjA1NTE2OTAyfQ.ozioLM26fGVO8SfqfZllwp8j6Gy_PWoDsntPzlvXor0                                                                                                                    14:25:53
    这里执行 store.token.save = Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI0OWYxMjgwZTkxODRjZmMyIiwiZXhwIjoxNjA1NTE2OTAyfQ.ozioLM26fGVO8SfqfZllwp8j6Gy_PWoDsntPzlvXor0                                                                                                                      14:25:53
    Making request to /api/me                                                                                                                                                                                                                                                                           14:25:53  
    这里执行 computed null                                                                                                                                                                                                                                                                              14:25:53  
    这里执行 store.user.save = {"id":"49f1280e9184cfc2","nickname":"test","avatar":"https://vip1.loli.net/2020/11/15/Eqlr5T7JcmOuDWk.jpg"}                                                                                                               14:25:53
    

    为啥在 index.vue 的 computed 里面 this.$store.state.user 会比 nuxtServerInit commit("saveUser", res.data.data)想执行啊?

    为什么会出现这个问题,以及怎么解决

    2 条回复    2020-11-16 14:59:12 +08:00
    lxzxl
        1
    lxzxl  
       2020-11-16 14:54:31 +08:00   ❤️ 1
    你的 nuxtServerInit 没有返回 Promise, 改成 `return this.$axios.post...`
    id4alex
        2
    id4alex  
    OP
       2020-11-16 14:59:12 +08:00
    @lxzxl 老铁...破案了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2317 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:55 · PVG 14:55 · LAX 23:55 · JFK 02:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.