简体   繁体   English

组件不匹配 在 Vue.js 中正确存储数据

[英]Component not matching Store data right in Vue.js

I have an component that tries to match the route param to an array of objects in the store to see if the route name/variable is in an object and then display data from the object if it matches.我有一个组件尝试将路由参数与存储中的对象数组匹配,以查看路由名称/变量是否在 object 中,然后显示来自 object 的数据(如果匹配)。 However, no matter what, it returns false always.但是,无论如何,它总是返回 false。 It is pulling its data from an external JS file, so could this be something with it being passed by reference rather than hard defining it?它正在从外部 JS 文件中提取数据,所以这可能是通过引用传递而不是硬定义它吗?

Here is my store这是我的商店

import Vue from "vue";
import Vuex from "vuex";
import { productList } from "../assets/db.js";

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    productList: productList
  },
  mutations: {},
  actions: {},
  modules: {}
});

export default store;

Here is the function inside the component这是组件内部的 function

created: function() {
        for (let type in this.$store.state.productList) {
          console.log(type.includes(this.$route.params.ptype))
        }
}

The route is dynamic and matches ptype ( this.$route.params.ptype )路由是动态的并且匹配ptype ( this.$route.params.ptype )

However it always returns false in console.但是它总是在控制台中返回 false。 Is there something wrong with the loop?循环有问题吗? Also, if there is a better way to display data using routes like this please let me know!另外,如果有更好的方法来使用这样的路线显示数据,请告诉我! Thanks!谢谢!

We are lacking some information here, productList is supposedly an Array of Objects?我们这里缺少一些信息, productList应该是一个对象数组? then inside of your loop, type would be an Integer?那么在你的循环内部, type将是 Integer? Are you sure that it isnt a string?.你确定它不是一个字符串吗? Incase i misunderstood and productList is just an Object, then that would mean type is a string.万一我误解了, productList只是一个 Object,那么这意味着type是一个字符串。 Now, String.protoype.includes() is case-sensitive, so perhaps this is an Issue of case-sensitivity?现在, String.protoype.includes()区分大小写,所以也许这是区分大小写的问题? If not i would go ahead and double check the shape of your data, perhaps youve mixed something up.如果不是,我会提前 go 并仔细检查数据的形状,也许你混淆了一些东西。

In your vue-router config, there is a flag you can set the flag per route props , if you set that to true, all params will be set as props and you can access them with this.myProp inside of your component.在您的 vue-router 配置中,有一个标志,您可以为每个路由props设置标志,如果将其设置为 true,则所有参数都将设置为 props,您可以使用组件内部的 this.myProp 访问它们。 You can also pass in an Object which will pass in static props.您还可以传入 Object ,它将传入 static 道具。 Or you can pass a function, which takes the route object youve been using in your component as parameter to do whatever you wish with it before passing them as props.或者,您可以传递一个 function,它采用您在组件中使用的路由 object 作为参数,在将它们作为道具传递之前用它做任何您想做的事情。

More about that in the vue-router docs更多关于vue-router文档的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM