Polymer Web Component中的Bootstrap下拉/导航栏切换

时间:2014-12-01 23:59:53

标签: twitter-bootstrap polymer web-component

我正在尝试将Bootstrap与基于Polymer Web Components的应用程序一起使用。基于这个问题:Bootstrap.js not working in Polymer components我通常可以通过在组件的模板中包含它来使Bootstrap工作,以便它可以看到影子DOM内容。

然而,即便如此,似乎下拉菜单和导航栏切换等功能仍无效。有解决方法吗?

我创建了一个试图证明这一点的JSFiddle。仅当Bootstrap JS / CSS未包含在聚合物组件中时,全局下拉列才会起作用。如果Bootstrap JS / CSS包含在聚合物组件中,则组件的下拉列表将部分正确设置样式,但实际上不起作用。这也将破坏全球下拉列表的功能。

http://jsfiddle.net/u72xk3kk/

<link rel="import" href="http://www.polymer-project.org/components/polymer/polymer.html">

<nav class="navbar-default" role="navigation">
    <article class="container-fluid">
        <header class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <span class="navbar-brand">NavBar</span>
        </header>
        <section class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
                <li><a>Foo</a></li>
                <li><a>Bar</a></li>
            </ul>
        </section>
    </article>
</nav>

    This dropdown works fine if the bootstrap js and css includes are not done in the component's template:<br/>
    <div id="works" class="dropdown">
        <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">
            Global Dropdown
            <span class="caret"></span>
        </button>
        <ul id="blarg" class="dropdown-menu" role="menu">
            <li>Foo</li>
            <li>Bar</li>
        </ul>
    </div>     

<polymer-element name="my-component">
    <template>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"/>
        <div class="dropdown">
        <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">
            Component Dropdown <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" role="menu">
            <li>Eggs</li>
            <li>Spam</li>
        </ul>
    </div>   
    <content/>
    </template>
    <script>
        Polymer({});
    </script>
</polymer-element>

    This dropdown is partially styled ok, but doesn't actually work even if the Bootstrap js and css is included in the polymer component templae:<br/>
<my-component/>

1 个答案:

答案 0 :(得分:0)

        ready: function () {
            $('.dropdown-toggle').dropdown();
        }

我将js文件仅包含在页眉中,而不是在我的项目中的组件中,并且我在页面和组件中都有工作下拉列表。自举/ JS / dropdown.js&#34;