0
votes

I'm new to svelte and need a bit of help. I have a weight conversion function that calculates conversion between pounds and kilograms. There are two inputs and entering a value in pounds will automatically show kilos and vice versa.

let k = '';
let l = '' ;

function setBothFromK(value) {
k = +value;
l = +( k * 2.2046226218).toFixed(1);
}
function setBothFromL(value) {
l = +value;
k = +( l / 2.2046226218).toFixed(1);
}

and the inputs are set like this:

 <input id="inputKilograms" name="inputKilograms" type="number" placeholder="kgs"
  bind:value={k} on:input="{e => setBothFromK(e.target.value)}" min="0">


 <input id="inputPounds" name="inputPoundsE" type="number" placeholder="lbs" 
 bind:value={l}  on:input="{e => setBothFromL(e.target.value)}" min="0">

I then use the value k to calculate a drug dosage in mg and mls with values provided from an array drugs. When the weight changes, the dosages and volumes change.

The calculated dosages and volumes are set up like this:

    {#each antibiotics as antibiotic, i}

    <div>{((k * antibiotic.dosevalue) / antibiotic.concs).toFixed(2)}</div>

     <div> {(k *   antibiotic.dosevalue).toFixed(2)} </div>

    {/each}

EDITED and updated REPL

It works well. What I'm trying to do is set up a svelte component so I can export both the html and the 2 functions to be used in a number of different pages.

The Weightconvert.svelte is:

   <script>

  let  k = ''; 
  let  l = '';

  export function setBothFromK(value) {
  k = +value;
  l = +( k * 2.2046226218).toFixed(1);
  }

   export function setBothFromL(value) {
  l = +value;
  k = +( l / 2.2046226218).toFixed(1);
  }

  </script>

  <input id="inputPounds" name="inputPounds" type="number" placeholder="lbs" 
 bind:value={l}  on:input="{e => setBothFromL(e.target.value)}" min="0">

  <input id="inputPounds" name="inputKilograms" type="number" placeholder="kgs" 
 bind:value={k}  on:input="{e => setBothFromK(e.target.value)}" min="0"`

The App.svelte is:

   <script>
   let k = '';
   let l = '' ;

   import Weightconvert, {setBothFromK , setBothFromL } from './Test.svelte';
   let dosevalue = 20;
   let concs = 3;
   </script>

  <Weightconvert ></Weightconvert>

     <div bind:this={k}> Dose: {(k * dosevalue).toFixed(2)}</div>

     <div  bind:this={k}> Volume: {((k * dosevalue) / concs).toFixed(2)} </div>

The weight converter function works in each page but no calculations of dosage and volume occur. Being a novice, I'm unsure how I can get that to work. Thanks for any help.

Here is a REPL with the basic functionality but without the component import

3

3 Answers

0
votes

To export a function from a component you have to bind it in the calling parent and then it will available everywhere else within that parent

<Weightconvert bind:this={myweightconvert}></Weightconvert>
<input .... on:input="{e => myweightconvert.setBothFromK(e.target.value)}">

<script>
let myweightconvert;
</script>
0
votes

You can utilize the context="module" script tag to export functions from your components.

for example :

https://svelte.dev/docs#script_context_module

https://svelte.dev/examples#module-exports

0
votes

According to the guidelines I shouldn't be answering this question, but I felt inclined to jump in. Reading your post it seems there is quite some confusion on when to use certain parts of Svelte. For instance, you would only export module functions if you need to change components from Javascript. And since you have inputs that change the values, there's no need for that.

All the calculations and constants can be put inside the component and only the values that change per antibiotic can be changed through component attributes.

The reason why the calculations weren't working is because the variables were initialized as strings instead of numbers.

This is how I would approach it, based on my understanding of your use-case.