【问题标题】:Problem with template implementation (VUE) using .NET (MVC)使用 .NET (MVC) 的模板实现 (VUE) 问题
【发布时间】:2019-11-10 16:27:11
【问题描述】:

我正在尝试实现一个外部模板(创建一个 HTML 页面),但我无法成功。此页面是一个包含 Vue 应用程序的 ASP.NET MVC 页面。

我想将此组件的 template 部分移动到外部文件中,但每当我这样做时,它都不起作用。

以下(下)确实有效,但由于失去了文本编辑功能,维护或构建并不容易。

Vue.component('我的组件', { 模板:'#my-component' }

这是当前的代码,它运行良好:

var foo = Vue.component('foo', {
template:'
<table class="table">
 <template v-for="(foo, ColName, index) in foos">
    <template v-if="index % 3 == 0">
        <tr>
        </tr>
    </template>
    <th>{{ColName}}</th>
    <td v-if="foo">{{foo}}</td>
    <td v-else> -- </td>
 </template>
</table>',
data: function () { 
  return {
    foos: null,
    NumColuns: 3 
  }
}, 
mounted() { 
 axios
  .get('/api/account/foo/')
  .then(response => {
    this.foos = response.data                
   })
  .catch(error => {
    console.log(error1)
      this.errored = true
  })
  .finally(() => this.loading = false)
}
});
var foo = new Vue({ 
  el: '#foo-vue' 
});

【问题讨论】:

  • #my-component 表示模板位于该页面的 HTML 中。根据您的解释,尚不清楚 Vue 代码是在该页面上还是在其他文件上,或者到底是什么。您能否明确说明其中包含哪些文件?
  • 我认为他在一个 JS 文件中包含所有内容,包括模板,并且他从 HTML 页面引用该模板。他希望将模板放在单独的文件中。
  • 我所有的代码都在JS页面上,我想把它分离成一个HTML页面(模板)。

标签: javascript asp.net-mvc vue.js


【解决方案1】:

要从文件中获取 HTML 模板,您需要一个异步组件。

文档:https://vuejs.org/v2/guide/components-dynamic-async.html

在您的示例中,获取 HTML 并在 Vue.component 工厂中返回承诺。

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <script src="https://unpkg.com/vue"></script>
</head>

<body>

  <div id="app">
    <foo></foo>
  </div>

  <script>
    var foo = Vue.component('foo', () => {
      return fetch('/template.html')
        .then((response) => response.text())
        .then((html) => {
          return {
            template: html,
            data: function () {
              return { foos: null, NumColuns: 3 }
            },
            mounted() {
              this.foos = [{ foo: 1, ColName: "A" }];
              //  axios.get('/api/account/foo/').then(response => {
              //     this.foos = response.data
              //    })
              //   .finally(() => this.loading = false)
            }
          };
        });
    });
    var foo = new Vue({
      el: '#app'
    });
  </script>
</body>

</html>

模板.html

<table class="table">
 <template v-for="(foo, ColName, index) in foos">
    <template v-if="index % 3 == 0">
        <tr>
        </tr>
    </template>
    <th>{{ColName}}</th>
    <td v-if="foo">{{foo}}</td>
    <td v-else> -- </td>
 </template>
</table>

【讨论】:

  • 有没有办法像 Vue 文档中的 .vue 文件那样导入它?
【解决方案2】:

如果您的视图是剃须刀.cshtml 模板,那么您只需将模板提取到 html 文件中,然后将该文件作为原始 html 代码加载到您的 ASP.NET MVC 视图中,因为 Vue 模板是有效的 html:

<!-- in cshtml view -->
<script id="my-component" type="text/x-template">
    @Html.Raw(File.ReadAllText(Server.MapPath("~/Path/To/Template/my-component-template.html")))
</script>

并且在您的 Vue 组件中,它应该与您提到的 template: '#my-component' 一起使用。

文档:Vue: Edge Cases

附:您可以为您的组件准备一个局部视图或创建一些扩展方法以使代码更具可读性并添加一些缓存。

【讨论】:

    【解决方案3】:

    您可以做的最好的事情是配置整个 Vue 项目,这样您就可以使用 .vue 模板文件。我最近发现一篇文章详细解释了如何实现这一点,但它专门针对浏览器扩展。在这种情况下,您必须采取的实现它的步骤在大多数情况下是相同的,所以无论如何我都会在此处链接文章:https://medium.com/javascript-in-plain-english/how-i-built-a-browser-extension-with-vue-part-2-2c4ab2dd752d。

    基本上发生的事情是,您将使用 Webpack 自己配置 Vue 项目,以将 Vue 文件编译成一个 app.js 文件,然后您将在文件中引用该文件(在本教程中,此文件是他的 app.html )。然后它将其组件注入 #app 元素并处理所有虚拟 DOM 操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 2017-06-03
      • 2011-05-09
      • 2017-04-13
      相关资源
      最近更新 更多