Difficulty in loading TypeScript web component using Polymer serve

As I follow the LitElement guide provided here: , I meticulously adhere to each step mentioned and then execute polymer serve in my project's root directory.

I diligently clone every file from their example stackblitz pages. Is it possible that using polymer serve is not the correct approach?

Despite my efforts, all I encounter is a blank page, with no recognition of the customElement being displayed.

https://i.sstatic.net/MMIin.png

Here is my-element.ts:

import { LitElement, html, customElement, property } from 'lit-element';

@customElement('my-element')
export class MyElement extends LitElement {
  @property()
  foo = 'foo';

  render() {
    return html`
      <p>hi!</p>
    `;
  }
}

This is index.ts:

import './my-element.ts';

And here's the content of index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <script src="/node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js"></script>
    <script src="/node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script>

    <title>lit-element code sample</title>
  </head>
  <body>
    <my-element></my-element>
  </body>
</html>

Lastly, let's look at package.json:

{
  "name": "my-app",
  "version": "1.0.0",
  "license": "ISC",
  "dependencies": {
    "@webcomponents/webcomponentsjs": "^2.2.10",
    "lit-element": "^2.1.0"
  }
}

Answer №1

I made sure to copy and paste every file from their demonstration stackblitz pages

When working with Stackblitz TypeScript projects, there is a specific structure that needs to be followed. In this structure, index.ts serves as the starting point for the TypeScript code, automatically compiled and imported into the final result.

In a local setup, things are a bit different. You need to select a build tool to compile TS files and import the output into index.html:

<!DOCTYPE html>
<html lang="en>
  <head>
    <!-- ... -->
    <!-- The exact path may vary based on your build configuration -->
    <script src="./path/to/my-element.js" type="module"></script>
  </head>
  <body>
    <my-element></my-element>
  </body>
</html>

You have the option of using tsc directly or utilizing a module bundler like Webpack + ts-loader or Rollup + rollup-plugin-typescript2. The choice between these tools depends on the complexity of your project and the browsers you aim to support.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to determine a path connecting 3D coordinates?

Understanding this concept is better with visual aids. I have a set of green points located here: https://i.sstatic.net/V8VEC.png My goal is to determine several points along the red line shown below: https://i.sstatic.net/b26AK.png Although this illus ...

Angular 8 - How to properly await a promise's completion within a loop before moving on to the

I have a list that needs to be iterated through, with each iteration dependent on the completion of the previous one. Each iteration returns a promise. Currently, my code processes the list quickly without waiting for the promises to resolve: this.records ...

Collapse the accordion item when a different one is selected

Check out this code snippet that's working on jsfiddle I'm attempting to add a function to close any open accordion items when another one is clicked. I've added a class "open", but I'm having trouble removing the class when a differen ...

Several onclick events on a single webpage

Despite numerous attempts and hours of reading, I am still unable to figure this out. There are two different types of card elements, each with a selection of 15 different color choices. Aside from using "a", is there anything else I can try to solve this? ...

Express 4.14.1 experiencing issues with URL rewriting

I have come across several Stack Overflow posts suggesting that to rewrite a URL in Express 4, one would need to implement something similar to the code below: router.use('/one/:someId', (req, res, next) => { req.url = `/two/${req.params. ...

retrieve records in reverse chronological order with mongodb

In my project, I am inserting a large amount of historical data documents into a history collection. Since these documents are never modified, the order remains correct as no updates are made. However, when retrieving the data, I need them in reverse order ...

Looking to verify a disabled select element and adjust the opacity of that element when it is disabled

$_product = $this->getProduct(); $_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes()); ?> <?php if ($_product->isSaleable() && count($_attributes)):?> <dl> <?php foreach($_attrib ...

What could be causing the error I encounter when attempting to install axios?

While attempting to incorporate axios into my project, I used the command below: npm install axios However, an error has been persistently popping up: npm ERR! Unexpected end of JSON input while parsing near '...devDependencies":{"co' ...

When conducting TS code scanning, errors may not be returned, but TSLint will provide a comprehensive list

Experiencing an unusual issue - SonarQube is running for TS sources but not returning any errors. Utilizing the SonarTS plugin results in SonarQube reporting 0% errors. Any thoughts on what might be causing this? Thank you. The sonar-project.properties fi ...

Implement a callback function for unchecked checkboxes on change

Currently, I am working with a gridview that includes a checkbox field. My goal is to use jQuery to create a function that activates when an unchecked checkbox is checked. function clickAllSize() { alert("helloy"); } $(document).ready(function () { ...

Enhance event handling in Google Maps JS API v3 by setting priorities on event listeners

Is there a way to subtly prioritize event listeners without changing the order they were added in? See code snippet: var listener1 = function () { console.log('@listener1'); }, listener2 = function () { console.log(' ...

Getting HTML from Next.js middleware - a step-by-step guide

Is there a way to send the HTTP Status Code 410 (gone) together with a customized HTML message? I want to display the following content: <h1>Error 410</h1> <h2>Permanently deleted or Gone</h2> <p>This page is not foun ...

Guide on creating a custom type for an object utilizing an enum framework

Enumerating my shortcuts: export enum Hotkey { MARK_IN = 'markIn', MARK_OUT = 'markOut', GO_TO_MARK_IN = 'goToMarkIn', GO_TO_MARK_OUT = 'goToMarkOut' } I am now looking to define a type for a JSON ob ...

guide on adding a variable to the link_to path

In my attempt to incorporate a variable into the link_to function below: <%= link_to '<button type = "button">Players</button>' .html_safe, live_players_path(:Team => @tmf) %> Whenever I click on this link, it seems to ...

Adjust cursor location in a provided OnTypeFormattingEdits with Monaco Editor

I've implemented the following code to automatically close an XML tag when typing the '>' of an opening tag. Everything is working smoothly so far, however, I am trying to position the cursor between the tags instead of at the end of the ...

Struggling with navigating JSON data in JavaScript and facing difficulties sorting the array

I am currently facing the challenge of organizing data obtained from an API using JavaScript. JavaScript Code to Retrieve Data: function getResults() { var url = $.getJSON("http://api.api.com&leagues=SOCENGPRE&lang=en&format=jsonp&cal ...

Listen for the 'open' event on a node HTTP server

This question pertains to a previous inquiry I had about node httpServer encountering the EADDRINUSE error and moving to the next available port. Currently, my code looks like this: var port = 8000; HTTPserver .listen(port, function() { console.lo ...

`CSS Content Placeholder Issue When Used Alongside JavaScript`

Let me explain a bit, I have a master page named UserProfile.master which has a content placeholder linked to UserProfileWall.aspx. Now, I am trying to incorporate some additional JavaScript and a second CSS file in the userprofilewall page. However, whene ...

Transform a JSON array containing individual objects into a new JSON array with objects

My array contains objects with nested objects in each element, structured like this: [ { "person": { "name": "John", "isActive": true, "id": 1 } }, { "person": { "name": "Ted", "isActive": true, "id": 2 } } ] I ...

Using the theme object in makeStyles in Material UI without requiring a ThemeProvider – a step-by-step guide!

My custom theme object is structured as follows: import palette from './palette'; import typography from './typography'; const theme = createMuiTheme({ palette, typography, })); export default theme; When using MUI, useStyles() ...