0

我想知道如何在 lit-element/lit-html 中实现引导导航栏

我在 lit-element 中创建了导航栏组件,但导航栏下拉和切换不起作用。

我必须将固定顶部导航栏实现为 html 中的 lit-element 组件。请检查代码并卡住。如果有任何替代方案,请提供解决方案。

// component.js
import { LitElement, html } from 'lit-element';

class MyElement extends LitElement {
  render(){
    return html`
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
      <div>
            <header>
    <nav class="navbar navbar-expand-md navbar-light bg-light fixed-top">
      <a class="navbar-brand"><img src="../images/send4x.svg" alt="" class="logoC mr-4"></a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse justify-content-between" id="collapsibleNavbar">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item dropdown mr-4">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownProvider" data-toggle="dropdown"
              aria-haspopup="true" aria-expanded="false">
              Providers
            </a>
            <div class="dropdown-menu mr-4" aria-labelledby="navbarDropdownProvider">
              <a class="dropdown-item"
                href="#">trans</a>
              <a class="dropdown-item"
                href="#">fund</a>
            </div>
          </li>
                  </ul>
      </div>
    </nav>
  </header>
      </div>
    `;
  }
}
customElements.define('my-element', MyElement);


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">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>

  <script src="/node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js"></script>
  <script src="/node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script>

  <script type="module" src="./my-element.js"></script>
  <title>lit-element code sample</title>
</head>
<body>
  <my-element>  </my-element>
</body>
</html>

4

2 回答 2

1

如果您愿意放弃 Shadow DOM,可以通过覆盖 lit-element 类中的 createRenderRoot 方法将渲染根切换到元素而不是阴影根:

class Navbar extends LitElement {
    createRenderRoot() {
        return this;
    }
    // etc
}
于 2019-10-19T09:17:34.893 回答
0

您可以将 Bootstrap 与导入到 shadow dom 类的一些 3rd 方一起使用,但我认为直接方式可能使用lit-html如下:

演示

import { html, render } from 'lit-html/lit-html.js'; 


const myTemplate = ()=>  html`

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
      <div>
            <header>
    <nav class="navbar navbar-expand-md navbar-light bg-light fixed-top">
      <a class="navbar-brand"><img src="../images/send4x.svg" alt="" class="logoC mr-4"></a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse justify-content-between" id="collapsibleNavbar">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item dropdown mr-4">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownProvider" data-toggle="dropdown"
              aria-haspopup="true" aria-expanded="false">
              Providers
            </a>
            <div class="dropdown-menu mr-4" aria-labelledby="navbarDropdownProvider">
              <a class="dropdown-item"
                href="#">trans</a>
              <a class="dropdown-item"
                href="#">fund</a>
            </div>
          </li>
                  </ul>
      </div>
    </nav>
  </header>
      </div>

    `;

render(myTemplate(), document.body);
于 2019-05-30T14:26:43.510 回答