#hi, I am struggling with using a method in my TS file in my html

113 messages · Page 1 of 1 (latest)

woeful zephyr
#

Sorry I am struggling badly

drowsy acorn
#

!:ask

#

!ask

hushed vaporBOT
#
`!ask`:

It's always ok to just ask your question; you don't need permission. See #how-to-get-help for more tips.

woeful zephyr
#

Oh ok

#

So it says that it failed to load the module

#

That is my imported code

drowsy acorn
#

gonna need some more info to go off of here

#

can you show the error?

woeful zephyr
#

Sorry of course

#

Is it ok if I ask a js question?

drowsy acorn
#

!ask

hushed vaporBOT
#
`!ask`:

It's always ok to just ask your question; you don't need permission. See #how-to-get-help for more tips.

woeful zephyr
#

Ok so

#

import.meta.dir

#

Is works in js

#

But not in TS?

drowsy acorn
#

ts compiles down to js

woeful zephyr
#

What is disable picture in picture?

#

The main thing I am trying to do

#

Is I have a button called "Say hi"

drowsy acorn
#

please don't use photos of a screen

#

i can't even read the full error there

woeful zephyr
#

The button is defined in a html file called scanner.html

drowsy acorn
#

copied text would be ideal, if not then a screenshot, but please don't use photos.

woeful zephyr
#

Then in scanner.ts it has a function called say hi

drowsy acorn
woeful zephyr
#

In my build folder is:
Index.js
Scanner.js

#

Do I need to "build" scanner.html into the build folder as well?

#

I know you said no pictures, but I wanted to show you the webpage.

drowsy acorn
#

no, but you need to reference the built js files, not the src ts files

woeful zephyr
#

Thank you for helping me btw

woeful zephyr
#

One second

#

So scanner.html

#

<script type="module" src="..build/scanner.js>

#

Is what it should be ?

#

Instead of src=scanner.ts

drowsy acorn
#

well first off that references a path ..build, rather than ../build, which is what you might have meant?

#

secondly, the path here is relative to the url, it doesn't really know about your filetree

woeful zephyr
#

But then I get a yellow squiggly and a warning that file cannot be resolved.

#

I'm using intellij Ultimate btw

woeful zephyr
#

scanner.html is in src

#

Scanner/src/scanner.html

#

Scanner/src/scanner.ts

#

Scanner/build/index.js

#

Scanner/build/scanner.js

#

Scanner/index.ts

#

That's all my important files

drowsy acorn
woeful zephyr
drowsy acorn
#

is html for source code?

woeful zephyr
drowsy acorn
#

yeah

woeful zephyr
#

Should I move scanner.html to the main folder?

drowsy acorn
#

and ts is, so index.ts should also be in src, really

drowsy acorn
#

do you have an index.html?

woeful zephyr
#

No, I have a index.ts

drowsy acorn
#

so you're opening scanner.html directly?

woeful zephyr
#

That serves up scanner.html

drowsy acorn
#

i see

#

are you serving your entire project root as static files?

woeful zephyr
#

Thank you for being patient with me

#

Static files? I think so?

#

I mean, at the moment I just want to be able to print "hi"

#

It works when it's in the html file but not when it is in its own file

drowsy acorn
#

make sure you're actually getting the scanner.js

#

since scanner.html is in the root, that would be build/scanner.js

#

(sidenote though; seems like this file structure could use a redesign)

woeful zephyr
#

Scanner.html’s contents:

<!DOCTYPE html>
<html lang="en">
<body>
<script type="module" src="../build/scanner.js"></script>

<video></video>

<h1> Scanner </h1>

<button type="button" class="btn btn-primary" onclick="startScanning()">Start Scanning!</button>
<br>
<button type="button" class="btn btn-primary" onclick="stopScanning()">Stop Scanning!</button>
<br>
<button type="button" class="btn btn-primary" onclick="sayHi()">Hi!</button>
<br>
<button type="button" class="btn btn-primary" onclick="function sayHey() {
console.log('Hey!');
}
sayHey()">Hi (in file JS)!</button>
<br>
</body>
</html>

woeful zephyr
drowsy acorn
#

i don't do webdev much, much less with ts, so i don't know what the typical project structure for a web project using ts without any frameworks would be

#

i can only tell you what would work here, unfortunately

woeful zephyr
#

I feel like this should be easy

#

And thus I am just being stupid

drowsy acorn
woeful zephyr
#

Is that not right?

drowsy acorn
#

no, modules act as individual components, while scripts put their declarations in the global scope for all to access

#

(they both use the script tag though)

woeful zephyr
#

So remove the type bit

#

Ok

#

Ok now I am getting insane errors

drowsy acorn
#

well the file itself also has to be a script instead of a module

woeful zephyr
#

Oh ok

#

How do I do that ?

#

Scanner.ts’s contents:

import QrScanner from 'qr-scanner';
// import QrScanner from '/qr-scanner/qr-scanner.min.js';
// do something with QrScanner.

// To enforce the use of the new api with detailed scan results, call the constructor with an option object, see below.
const videoElem : HTMLVideoElement = '';
const qrScanner = new QrScanner(
videoElem,
result => console.log('decoded qr code:', result),
{ /* your options or returnDetailedScanResult: true if you're not specifying any other options */ },
);

export function startScanning() {
qrScanner.start();
}

export function stopScanning() {
qrScanner.stop();
}

export function sayHi() {
console.log('Hi!')
}

drowsy acorn
#

ok yeah that's set up fully as a module

#

since it has that import though, seems like you want it to be a module

#

thonk yeah im not quite sure how to use modules with attribute events, since as aforementioned don't do webdev much

woeful zephyr
#

Oh ok

#

@drowsy acorn this is the code

drowsy acorn
#

yeah no thanks

#

not going to go to a random drive for that

woeful zephyr
#

It's just a word doc

#

Sorry

drowsy acorn
#

if you'd like to share, use bins (pastebin, srcshare, etc) or git (repo or gist) please

drowsy acorn
drowsy acorn
#

you could just upload the file directly.

#

that would be more handy

woeful zephyr
#

Sure

drowsy acorn
#

(or preferably upload them to dedicated code-sharing sites as mentioned before)

woeful zephyr
woeful zephyr
#

I'm back 😁