html - 如何使用 vue.js 获取本地 html 文件?

标签 html iis vue.js visual-studio-code single-page-application

我正在关注 this example

我正在尝试使用 vue.js 加载一个 html 文件并添加到我的模板中。

我的尝试:

HTML:

<html lang="en">

<head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="/Static/content/css/foundation.css">
    <link rel="stylesheet" type="text/css" href="/Static/content/css/spaceGame.css">

</head>

<body>
    <div id="app">
        <div class="grid-container">
            <div class="grid-x grid-padding-x">
                <div class="large-12 cell">
                    <h1>Welcome to Foundation</h1>
                </div>
            </div>
        </div>

        <div class="grid-x grid-padding-x">
            <div class="large-4 columns"></div>
            <div class="large-8 columns">
                <component :is="currentView"></component>
            </div>
        </div>

    </div>
    <!-- Footer -->
    <script src="https://unpkg.com/vue"></script>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="/Static/scripts/foundation.js"></script>
    <script type="text/javascript" src="/Static/scripts/what-input.js"></script>

    <script type="text/javascript" src="/Static/scripts/space.js"></script>
</body>

</html>

脚本:

$(function() {
    $(document).foundation()

    var myData = '../../Views/login.html';
    Vue.component('manage-posts', {
        template: myData,
    })

    Vue.component('create-post', {
        template: '#create-template'
    })

    new Vue({
        el: '#app',
        data: {
            currentView: 'manage-posts'
        }
    })

});

错误:

上面我得到以下内容:

  • Component template requires a root element, rather than just text.

更改 var myData = '../../Views/login.html';

 var myData = '<div>../../Views/login.html</div>';

摆脱了那个错误但是...我如何加载实际的 html 文件?

我是单页应用程序和 vue.js 的新手,现在已经有一段时间了,无法弄清楚。

编辑:

我要加载的 html 文件:

<div>
    <template id="manage-template">

    <form>
        <div class="sign-in-form">
            <h4 class="text-center">Sign In</h4>
            <label for="sign-in-form-username">Username</label>
            <input type="text" class="sign-in-form-username" id="sign-in-form-username">
            <label for="sign-in-form-password">Password</label>
            <input type="text" class="sign-in-form-password" id="sign-in-form-password">
            <button type="submit" class="sign-in-form-button">Sign In</button>
        </div>
    </form>
</template>
</div>

编辑 2:

如果这对答案有任何影响,我只想指出: 使用 VS-code,站点在 IIS 中运行,这里没有使用 node.js。

最佳答案

Vue 提供了一种 asynchronously creating 的方法一个组件。在这种情况下,您可以使用它从服务器检索您的模板。

在此示例代码中,我将使用 axios 检索模板,但您可以使用您喜欢的任何库。

Vue.component('manage-posts', function(resolve, reject){
  axios.get("../../Views/login.html").then(response => {
    resolve({template: response.data})
  })
})

关于html - 如何使用 vue.js 获取本地 html 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46180047/

相关文章:

javascript - JS Keydown 在第一个事件之后触发事件之前有一些奇怪的 1 秒延迟

html - 为什么 table th 字体垂直居中在 css 中不起作用?

javascript - 为什么人物在 Canvas 上显得扭曲?

c# - 如何创建具有始终运行组件的 WCF 服务?

node.js - 为什么对 Babel 和 ESLint 使用 Vue CLI?

javascript - 将 Laravel 数据传递给 Vue 组件

javascript - 如何访问 svg 内部 id 并让 CSS 更改该 id 的填充颜色?

html - CSS 和 HTML 水平弹出菜单 - 右侧定位的弹出菜单

javascript - 我无法刷新外部 javascript 文件

php - ActiveCollab 2.3.1 : Using SVN integration