Still up the learning curve.
I have this progessComponent that acts like a common utility for other components. As the name suggest, it tracks the progress of an activity
progressComponent.js
import { ref } from "vue"
export default {
setup() {
var self = this
const progressBarVal = ref(0)
function makeProgress(inVal) {
if (progressBarVal.value < 100) {
progressBarVal.value += inVal
console.log("progress" + progressBarVal.value)
}
}
function clearDiv(elementID) {
//some code here
}
return {
progressBarVal,
makeProgress,
clearDiv
}
}
}
bdggtt.js
Component for reading couple of .json
s and processing them
import {
ref,
defineProps,
onMounted,
shallowRef,
shallowReactive,
reactive,
computed
} from "vue"
export default {
setup() {
onMounted(async () => {
//Read Socials Json
//makeProgress(10)
//Read activityJson
//makeProgress(10) //Total 20
})
var self = this
const message = ref("")
const pgSocialsData = []
const pgSocials = shallowRef(pgSocialsData)
function getPgSocials(inPgLst, inTitle) {
//Process
//makeProgress(10) //Total 30
}
function getPgConnections(inPgLst, inTitle) {
//Process
//makeProgress(10) //Total 40
}
function getActivity(inPgLst, inTitle) {
//Process
//makeProgress(10) //Total 50
}
return {
message,
pgSocials,
getPgData
}
}
}
readbillSub.js
Does another set of activities
import {
ref,
defineProps,
onMounted,
shallowRef,
shallowReactive,
reactive,
computed
} from "vue"
export default {
setup() {
onMounted(async () => {
console.log("Hello")
progressBarVal.value = 25
let dataURL = "./_ini/billjson.json"
//makeProgress(10) //Total 60
message.value = "3"
message.value = "contact"
const params = new URLSearchParams(window.location.search)
if (params.has("id")) {
console.log("Had id")
console.log(params.get("id"))
message.value = params.get("id")
}
})
var self = this
const userPostData = []
const userPosts = ref(userPostData)
const message = ref("")
function getSubMenu(pgMenus) {
//some code here
//makeProgress(10) //Total 80
}
function getPgData(inPgLst, inTitle) {
//some code here
//makeProgress(10) //Total 90
}
return {
message,
userPosts,
getSubMenu
}
}
}
And main.js
import { createApp } from "vue"
//The below is wrong
import appPgFunc from "./vuejs/progressComponent.js"
const appPg = createApp(appPgFunc)
appPg.mount("#PgBar")
import appUserFunc from "./vuejs/bdggtt.js"
const appUser = createApp(appUserFunc)
appJson.mount("#contdSpace")
import appBillsFunc from "./vuejs/readbillSub.js"
const appBills = createApp(appBillsFunc)
appBills.mount("#hdrSpace")
I am not using build setup.
In progressComponent.js I have this function makeProgress
. I want to call it from bdggtt.js and readbillSub.js and any other components. How can I do it?
You're looking for one of the cornerstones of Vue 3's architecture, a composable.
Create a progress-bar.js file and place the progress bar logic inside of it:
import { ref } from "vue"
const progressBarVal = ref(0)
export const useProgressBar = () => {
function makeProgress(val) {
if (progressBarVal.value < 100) {
progressBarVal.value += inVal
console.log("progress" + progressBarVal.value)
}
}
return {
progressBarVal,
makeProgress
}
}
And now update progressComponent.js to:
import { useProgressBar } from "./progress-bar"
export default {
setup() {
const { progressBarVal, makeProgress } = useProgressBar()
function clearDiv(elementID) {
//some code here
}
return {
progressBarVal,
makeProgress,
clearDiv
}
}
}
In any other component where you need makeProgress
, simply import useProgressBar
, call it and get makeProgress
from the returned value.
Important note: I haven't actually tested the code, I hope it doesn't have any name mismatches or typos, I've written it on the fly, to outline the principle.