[Vuejs]-Pass data from vue file to js file in vuejs?

2👍

Simplest way is:

Product.vue

<script>
import { initData } from "./productData.js";

export default {
  name: "Product",
  props: {
    msg: String,
  },
  data() {
    return {
      apiRes: "",
    };
  },
  mounted() {
    // your api call
    this.apiRes = "Hi from server";
    initData(this.apiRes);
  },
};
</script>

productData.js

let data = null;

function initData(apiRes) {
  data = apiRes;
  console.log(data); // Output: "Hi from server"
  // Do something with Data
  
  doSomethingWithData();
}

function doSomethingWithData() {
  // Your app logic that depends on data
  // Here data will have value from API
}

// Here data is always null

export { initData };

Leave a comment