Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I access data from a js file?

Test.vue

<template>
      <div>
        <button @click="test()">test</button>
      </div>
    </template>
    <script>
    import logs from './data.js'
    export default {
        data () {
            return {
            logitems1: logs.data.infomation,
            logitems2:
          {
            log1d: 1,
            logDetail: "This is some log detail for log 1",
            logType: "general",
            createdBy: "name",
            CreatedAt: "date",
          },
        }
        },
        methods:{
          test(){
             console.log(this.logitems1.log1d)
             console.log(this.logitems2.log1d)
          }
        }
    }
    </script>

data.js

export default {
  data() {
    return {
      infomation: {
        log1d: 1,
        logDetail: "This is some log detail for log 1",
        logType: "general",
        createdBy: "name",
        CreatedAt: "date",
      },
    };
  },
};

Access to logitems2 is possible but I can’t access the data in logitems1, which has all the same information. I’m trying to access data from files other than components, which I can’t figure out how to access.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have to make a functional call.

<script>
    import logs from './data.js'
    export default {
        data () {
            return {
            logitems1: logs.data().infomation,
            logitems2:
          {
            log1d: 1,
            logDetail: "This is some log detail for log 1",
            logType: "general",
            createdBy: "name",
            CreatedAt: "date",
          },
        }
        },
        methods:{
          test(){
             console.log(this.logitems1.log1d)
             console.log(this.logitems2.log1d)
          }
        }
    }
    </script>

But if you just only want the data to be available exporting the object from js file without function would be better approach.

logdata.js

export default logInformation{
    log1d: 1,
    logDetail: "This is some log detail for log 1",
    logType: "general",
    createdBy: "name",
    CreatedAt: "date",
}

and then directly use as

import logInformation from "logdata.js"

console.log(logInformation)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading