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

Thu*_*3eR 8 html iis single-page-application vue.js visual-studio-code

我正在关注这个例子

我正在尝试使用 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>
Run Code Online (Sandbox Code Playgroud)

脚本:

$(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'
        }
    })

});
Run Code Online (Sandbox Code Playgroud)

错误:

上面我得到以下信息:

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

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

 var myData = '<div>../../Views/login.html</div>';
Run Code Online (Sandbox Code Playgroud)

摆脱了那个错误但是...我如何加载实际的 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>
Run Code Online (Sandbox Code Playgroud)

编辑2:

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

小智 7

     <div v-html="compiledHtml"></div>

data: function() {
    return {
      fileName: "terms.html",
      input: ""

    };
  },
     created() {
        this.fileName = this.$route.params.fileName;
        this.loadFile()
    }
    computed: {
        compiledHtml: function() {
          return this.input;
        }
      },
    methods: {

        loadFile() {
          axios({
            method: "get",
            url: "../../static/" + this.fileName
          })
            .then(result => {
              this.input = result.data;
            })
            .catch(error => {
              console.error("error getting file");
            });
        }
      },
Run Code Online (Sandbox Code Playgroud)

此代码有效。诀窍在于计算变量


Ber*_*ert 6

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

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

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