sessionStorage 实现页面/组件之间的数据传输

2017-08-08  本文已影响54人  YZY君
//articleComp.vue
sessionStorage.authorName=this.article.author.loginname;//保存作者名,在sideSec.vue中使用
//sideSec.vue
var authorName = sessionStorage.getItem("authorName");
this.$http({
          url: `https://cnodejs.org/api/v1/user/${authorName}`,
          method: 'get',
        }).......

但是会有个问题,组件sideSec.vue可能会比articleComp.vue先加载。导致两个组件中作者信息不一样。可用setTimeout()延迟加载。vuex应该可以解决,但是我还没用过vuex,暂时先这样处理。

      setTimeout(()=>{//延迟加载,权宜之计。在sessionStorage更新之后再读取。
        var authorName = sessionStorage.getItem("authorName");
        //      console.log(authorName)
        this.$http({
          url: `https://cnodejs.org/api/v1/user/${authorName}`,
          method: 'get',
        }).then((res) => {
          this.userInfo = res.data.data;
        }).catch((res) => {
          console.log('user info: ', res);
        });
      },300)

但还有另一个问题。由于router-view是复用的,两个页面中有相同的组件时,该组件不会刷新。

上一篇 下一篇

猜你喜欢

热点阅读