我在关注this example

我正在try 用vue加载html文件.js并添加到我的模板中.

我的try :

HTTML:

<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 到 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'
        }
    })

});

错误:

在上面,我得到了以下信息:

  • 组件模板需要根元素,而不仅仅是文本.

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

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

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

I am new 到 single page applications and 到 vue.js, been at this for some time now and can't figure it out.

编辑:

The html file that I am trying 到 load:

<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">
            <but到n type="submit" class="sign-in-form-but到n">Sign In</but到n>
        </div>
    </form>
</template>
</div>

编辑2:

If this has any impact on answers i just want 到 point out: using VS-code, site is running in IIS, no node.js is used here.

推荐答案

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

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

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

Vue.js相关问答推荐

如何在AXIOS调用响应中动态导入视频并创建它的绝对路径?

如何在Vue 3中将动态特性从主零部件同步到子零部件

Pinia+Vue 3状态react 性-StoreToRef的替代方案

为什么发送 POST API 请求时主体对象没有react ?

为什么 Pinia/Vuex 比使用服务类的classic 方法更受欢迎?

Nuxt 和 Vite 有什么区别?

如何使用 vuejs 在浏览器 url 中获取当前 product.id 的详细信息

如何在 VUE.js 中重新刷新时保持多层 b-collapse 菜单打开?

vue 模板中无法识别 pug 语言

如何 for each 用户分组聊天消息?

无需直接 DOM 操作即可绘制d3-axis

Vuetify 置顶工具栏

vuejs中watch方法和计算方法有什么区别

VueJS + VueRouter:有条件地禁用路由

Vuex - 如何跨不同选项卡持久存储更新?

单击复选框后,V-model 未更新

等待来自 WDS 的更新信号

如何从 vue.js 捕获 Jquery 事件

将 ref 的内容从子组件传递到另一个 VueJS 子组件

用于 nuxt.js 构建的自定义 index.html