当前位置: 首页 > 知识库问答 >
问题:

使用vue-router在浏览器上加载Vuexstore.state的内容

锺离韬
2023-03-14

在阅读了Vue、Vuex和Vue路由器的许多示例和文档后,我完成了以下项目:https://jsfiddle.net/junihh/30wda1em/5/

当我尝试从Post部分加载一行时,即使一切正常,Vuex存储中的值也是空的。以下是组件:

const PostContent = Vue.component('postcontent', {
    data() {
        return {
            post: this.$store.state.currentPost
            // post: {
            //     title: 'The title',
            //     content: 'The content for test.'
            // }
        }
    },
    template: getTemplate('#tpl-postcontent')
});

这里是更新state.currentPost值并调用"postContent"组件的组件。

const Posts = Vue.component('posts', {
    data() {
        return {
            url_path: '/posts/content',
            rows: this.$store.state.dataAll
        }
    },
    methods: {
        openPost: function(e)
        {
            let rowID = e.currentTarget.getAttribute('data-rowid');

            let dataAll = this.$store.state.dataAll;
            let currentPost = dataAll.filter(row => (row.id == rowID))[0];

            this.$store.state.currentPost = currentPost;
        }
    },
    template: getTemplate('#tpl-posts')
});

这里有人帮忙吗?我被困在那个问题上了。

共有1个答案

慎望
2023-03-14

您需要使用计算属性从您的商店收集信息,包括:

const PostContent = Vue.component('postcontent', {
  computed: {
    post() {
      return this.$store.state.currentPost
    }
  },
  template: getTemplate('#tpl-postcontent')
});

还要尽量避免在变异处理程序之外变异状态。您可以添加一个变异来设置currentPost,如下所示:

<template id="tpl-posts">
  ...
    <li v-for="row in rows" :key="row.id">
      <router-link :to="url_path" @click.native="openPost(row.id)">
        {{ row.title }}
      </router-link>
    </li>
  ...
</template>
const Posts = Vue.component('posts', {
  //...
  methods: {
    openPost: function(id)
    {
      this.$store.commit('SET_CURRENT_POST', id)
    }
  },
  template: getTemplate('#tpl-posts')
});
const store = new Vuex.Store({
  state: {
    dataAll: {},
    currentPost: {}
  },
  mutations: {
    SET_CURRENT_POST: function(state, id) {
      let post = state.dataAll.find(data => data.id === id)
      state.currentPost = post
    }
  }
});

小提琴

 类似资料:
  • 我正在使用selenium使用chrome浏览器来自动化网页,但是chrome浏览器没有启动,它在地址栏中显示“数据:;”而没有加载任何页面。 selenium: selenium-服务器-独立-3.0.0-beta2,也尝试了2.53 Chrome驱动程序:2.23(http://chromedriver.storage.googleapis.com/index.html?path=2.23/)

  • 本文向大家介绍vue-content-loader内容加载器的使用方法,包括了vue-content-loader内容加载器的使用方法的使用技巧和注意事项,需要的朋友参考一下 当我们开发网站或者APP时,遇到内容过多加载速度慢时,会导致用户打开页面有大量空白页,vue-content-loader正是解决这个问题的一个组件,使加载内容之前生成一个dom模板,提高用户体验。 第一步:安装 在控制台的

  • 我真的很讨厌instagram应用内浏览器。它显示的内容几乎不同于所有普通浏览器。出于某种原因,我的图像无法加载。 我的网站链接到其他网站的图像。但是这些图像没有扩展。(不确定这是否是问题所在) 这些图片不会加载到instagram应用程序内浏览器中。但在所有其他浏览器中,图像加载没有问题。 此外,我的网站是https,而图像是HTTP。不确定这是否有影响。 有没有办法在instagram应用内浏

  • 当使用React-Router时,有什么方法可以防止在浏览器的地址栏中显示?那是ReactJS。也就是说,单击链接转到新路由会显示或。取决于路线。

  • 我试图实现延迟加载,但得到如下错误** 错误错误:未捕获(在promise中):错误:BrowserModule已加载。如果您需要从一个延迟加载的模块中访问一些公共指令,比如NgIf和NgFor,那么可以导入CommonModule。 ** 需要帮助这是我的模块 共享模块 2.设置模块 5.AppModule

  • 基本用法 管理前端模块 生成前端模块 脚本文件的实时生成 browserify-middleware模块 参考链接 随着JavaScript程序逐渐模块化,在ECMAScript 6推出官方的模块处理方案之前,有两种方案在实践中广泛采用:一种是AMD模块规范,针对模块的异步加载,主要用于浏览器端;另一种是CommonJS规范,针对模块的同步加载,主要用于服务器端,即node.js环境。 Brows