简体   繁体   English

如何将字符串导出到另一个Vue文件?

[英]How do I export a string to another Vue file?

I have a masterData.js file that is a store for my master data, in short the file reads my mongo db data & sends it to other project components. 我有一个masterData.js文件,该文件存储我的主数据,简而言之,该文件读取我的mongo db数据并将其发送到其他项目组件。 I created a function to export the string in the masterData.js file as: 我创建了一个函数,将masterData.js文件中的字符串导出为:

/ ***************************** MUTATIONS
const mutations = {
exportColumns (payload) {
  Object.keys(payload[0]).map(x => { return x; });
 }
}

Where payload will store all the rows and payload[0] holds the value of column header names. 有效负载将存储所有行,有效负载[0]保留列标题名称的值。 The output of this chunk of code is like this: 此代码块的输出如下所示:

["_id","businessAreaName","businessAreaDisplay","councilDisplay","councilID"]

I want to transfer the values to masterData.vue file. 我想将值传输到masterData.vue文件。 My code on masterData.Vue is: 我在masterData.Vue上的代码是:

importColumns () 
  {
  let payload = {
    vm: this,
    mutation: 'masterData/exportColumns'
  };
}

What else should I add to to check whether the column names are received or not? 我还应该添加些什么来检查是否接收到列名?

If you're trying to access the data in your store from within a component, then you'll want to either just map the state to the component or map a getter to the component. 如果您试图从组件内部访问商店中的数据,那么您将希望仅将状态映射到组件或将吸气剂映射到组件。 Mutations are used by components (or by actions) to modify the state of the store. 组件(或操作)使用变异来修改存储的状态。 So instead you would do something like... 所以取而代之的是你会做...

//masterData.js
//assuming this gets rolled up as a module called masterdata to the primary store
//store for payload state
const state = {
  payload: null,
}

//allows payload to be set -- not sure how you are retrieving the payload but you can use this to store it however you get it
const mutations = {
  setPayload (state, payload) {
    state.payload = payload
  }
}

//get just the columns
const getters = {
  getColumns (state) {
    Object.keys(state.payload[0]).map(x => { return x; })
  }
}

export default {
  state,
  mutations,
  getters,
}

Then 然后

//masterData.vue
<template>
  //...
</template>

<script>
  import { mapGetters, mapState } from 'vuex'

  export default {
    computed: {
      //I believe getting state from a store module requires a function like this
      ...mapState({
        payload: function(state) {
          return state.masterdata.payload
        },
      }),
      //I think for getters you can just reference the method and it will find it
      ...mapGetters([
        'getColumns',
      ])
    },
  }
</script>

This is how you import stuff in a single file component. 这是在单个文件组件中导入内容的方式。

 <template> <!-- html stuff --> </template> <script> import Mutations from 'yourModule.js' export default { name: 'YourComponent', props: {}, data(){ return { foo: 'foo' } }, methods{ mymethod() { Mutations.exportColumn(this.foo); }, } } </script> 

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

相关问题 如何在vue中将html导出为ex​​cel? - How do I export html to excel in vue? 如何将此变量从类方法导出到另一个文件? - How do I export this variable from class method to another file? 如何导出 function 以在另一个文件脚本中调用 - How do I export a function to be called in another file script 如何从文件中导出字符串,并在导出的NODE.js文件中将require()导出到另一个文件中? - How can I export a string from a file and require() the exported string on another file in NODE.js? 如何正确地将一个普通的 JS 文件导出和导入到另一个 JS 文件中? - How do I correctly export and import a plain JS file into another JS file? 如何将对象数组及其属性导出到另一个 JS 文件? - How do I export an array of objects and it's properties to another JS file? 如何在单文件组件中扩展另一个 VueJS 组件? (ES6 vue-loader) - How do I extend another VueJS component in a single-file component? (ES6 vue-loader) NODEJS,我如何正确地将我的容器 class 的这个方法导出到我管理 express 的另一个 JS 文件? - NODEJS, how do I correctly export this method of my Container class to another JS file where I manage express? 如何从.vue文件导出多个对象 - How to export multiple objects from a .vue file 如何将此值导出到另一个 JS 文件中? - How can I export this value into another JS file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM