【问题标题】:How I can solve: HTML template must contain a single root element我如何解决:HTML 模板必须包含单个根元素
【发布时间】:2020-02-15 18:38:10
【问题描述】:

我正在尝试将温泉导航器与多个“.html”文件一起使用,但无论我尝试了什么,都会遇到同样的错误

我的场景

  • -www
  • /脚本
  • index.html
  • home.html

我想从 index.html 转到 home.html

错误:

Uncaught (in promise) Error: [Onsen UI] HTML template must contain a single root element
    at Object.Q.throw (onsenui.min.js:2)
    at Object.Q.createElement (onsenui.min.js:2)
    at onsenui.min.js:2

我的 index.html:

    <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsenui.css">
    <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsen-css-components.min.css">
    <script src="https://unpkg.com/onsenui/js/onsenui.min.js"></script>

    <script src='scripts/loginScript.js' type='text/javascript'></script>

    <link rel="stylesheet" href="style.css">
    <title>Tu Psicologa de Cabecera</title>
</head>
<body>
    <ons-navigator id="navigator" page="index.html"></ons-navigator>
        <ons-template id="index.html">
            <ons-page id="index">
                <img class="imagenLogo" src="https://i.imgur.com/cq7BbWn.png">

                <ons-card class="loginCard">
                    <ons-input id="userLogin" modifier="underbar" placeholder="Usuario" style="width: 100%; margin-top: 8px; margin-bottom: 8px;" float></ons-input>
                    <ons-input id="passLogin" modifier="underbar" placeholder="Contraseña" type="password" style="width: 100%; margin-top: 8px; margin-bottom: 8px;" float></ons-input>

                    <ons-button class="loginButton" onclick="loginNomral()">ENTRAR</ons-button>
                    <ons-button class="loginFacebookButton">ENTRAR CON FACEBOOK</ons-button>
                    <label class="labelRegister">REGISTRARME</ilabel>
                </ons-card>
            </ons-page>
        </ons-template>
    <script type="text/javascript" src="cordova.js"></script>
</body>

我的家.html

   <!DOCTYPE html>
   <html>
   <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

    <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsenui.css">
    <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsen-css-components.min.css">
    <script src="https://unpkg.com/onsenui/js/onsenui.min.js"></script>

    <link rel="stylesheet" href="style.css">
    <title></title>
</head>
<body>
    <ons-page id="home">
        <ons-toolbar>
            <div class="left"><ons-back-button>Page 1</ons-back-button></div>
        </ons-toolbar>

        <p>This is the second page.</p>
    </ons-page>
</body>
</html>

还有我的脚本:

    function loginNomral(){
        const navigator = document.querySelector('#navigator');
        navigator.resetToPage('home.html');

        ons.notification.alert('test');
    }

请帮忙 :v 编辑: 这是一个移动应用程序 完整的 html 视图: Image index.html Image home.html

【问题讨论】:

    标签: javascript html file navigator


    【解决方案1】:

    从您的代码看来,您有 &lt;/head&gt; 没有打开 &lt;head&gt;

    但主要问题是你不能有 2 个标签作为根,这意味着你需要用一个 &lt;html&gt;&lt;/html&gt; 标签包装它们

    像这样:

    <html>
       <head>
          <title>Page Title</title>
       </head>
       <body>
          <h1>My First Heading</h1>
          <p>My first paragraph.</p>
       </body>
    </html>
    

    【讨论】:

    • 您的意思是“似乎”,而不是“芝麻”吗?
    • @HereticMonkey 平心而论,不如芝麻。
    • 我删除了它,但仍然不行,我添加了指向文件图像的链接
    • @DaveNewton 这就是为什么我问而不只是编辑它;)
    【解决方案2】:

    我敢打赌,您正在创建某种组件……而这些组件看起来像这样:

    <header>...</header>
    
    <section>...</section>
    

    哪个...有 2 个“根”元素...对吗?

    <aside class='component-name'>
      <header>...</header>
    
      <section>...</section>
    </aside>
    

    这有 1 个。

    【讨论】:

    • 好的,那么,我如何将它应用到我的文件上?
    • 我对@9​​87654321@一无所知 - 但你应该问他们。
    【解决方案3】:

    Onsen UI navigator 与您指定页面内容的模板一起使用,您无需创建不同的 html 文件来指定不同的页面

    document.addEventListener('init', function(event) {
      var page = event.target;
    
      if (page.id === 'page1') {
        page.querySelector('#push-button').onclick = function() {
          document.querySelector('#myNavigator').pushPage('page2.html', {data: {title: 'Page 2'}});
        };
      } else if (page.id === 'page2') {
        page.querySelector('ons-toolbar .center').innerHTML = page.data.title;
      }
    });
       <!DOCTYPE html>
       <html>
       <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    
        <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsenui.css">
        <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsen-css-components.min.css">
        <script src="https://unpkg.com/onsenui/js/onsenui.min.js"></script>
    
        <link rel="stylesheet" href="style.css">
        <title>App</title>
    </head>
    <body>
        <ons-navigator swipeable id="myNavigator" page="page1.html"></ons-navigator>
    
    <template id="page1.html">
      <ons-page id="page1">
        <ons-toolbar>
          <div class="center">Page 1</div>
        </ons-toolbar>
    
        <p>This is the first page.</p>
    
        <ons-button id="push-button">Push page</ons-button>
      </ons-page>
    </template>
    
    <template id="page2.html">
      <ons-page id="page2">
        <ons-toolbar>
          <div class="left"><ons-back-button>Page 1</ons-back-button></div>
          <div class="center"></div>
        </ons-toolbar>
    
        <p>This is the second page.</p>
      </ons-page>
    </template>
    </body>
    </html>

    【讨论】:

    • 我知道,但是项目会很大,我不方便把所有的东西都放在一个html文件中以便以后维护
    • 好的,如果是这种情况,您将不得不编写自己的 pageLoader(我猜),您仍然可以使用模板(就像 angular 1 一样)遵循相同的模式,而不是创建整个 HTML 结构,查看 pageLoader 属性
    【解决方案4】:

    解决方法很简单,几个小时后,我删除了所有的 html 格式并保留了标签。

    从这里:

    <!DOCTYPE html>
    <html>
       <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    
        <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsenui.css">
        <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsen-css-components.min.css">
        <script src="https://unpkg.com/onsenui/js/onsenui.min.js"></script>
    
        <link rel="stylesheet" href="style.css">
        <title></title>
    </head>
    <body>
        <ons-page id="home">
            <ons-toolbar>
                <div class="left"><ons-back-button>Page 1</ons-back-button></div>
            </ons-toolbar>
    
            <p>This is the second page.</p>
        </ons-page>
    </body>
    </html>
    

    到这里:

    <ons-page id="home">
        <ons-card class="loginCard">
            <ons-input id="userLogin" modifier="underbar" placeholder="Usuario" style="width: 100%; margin-top: 8px; margin-bottom: 8px;" float></ons-input>
            <ons-input id="passLogin" modifier="underbar" placeholder="Contraseña" type="password" style="width: 100%; margin-top: 8px; margin-bottom: 8px;" float></ons-input>
    
            <ons-button class="loginButton" onclick="logintest()">ENTRAR</ons-button>
            <ons-button class="loginFacebookButton">ENTRAR CON FACEBOOK</ons-button>
            <label class="labelRegister">REGISTRARME</ilabel>
        </ons-card>
    </ons-page>
    

    【讨论】:

    • 其实你的问题不在于文档的结构。它带有模板ID。 navigator.resetToPage('home.html'); 看来,您正在调用一个不存在的 id。我在常规 html 页面中有温泉应用程序,它可以工作
    猜你喜欢
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    相关资源
    最近更新 更多