gpt4 book ai didi

javascript - 如何在我的 .cshtml 页面中调用 Vue 组件?

转载 作者:搜寻专家 更新时间:2023-10-30 22:17:39 24 4
gpt4 key购买 nike

我有一个结合了 Vue.js 的 ASP.NET Core MVC 项目。我只使用 Vue.js 来使我的页面具有反应性。我的目标是基于 _layout.cshtml 文件中的 div 创建一个主 vue 实例,然后使用 X-Templates 创建 Vue 组件并在我的网络应用程序中使用它们。值得一提的是,我希望必须在其自己的 .js 文件中包含 JS Vue.Component() 逻辑,因为其中一些组件将具有大量的 js 逻辑。

我遇到的问题是,当我从我想要使用它们的 .cshtml 中调用它们时,让 Vue 注册/识别我的组件。我找到了这个 stackoverflow 帖子 link显示了使这项工作的逻辑应该如何,但他的示例显示了 cshtml 中的 JS 逻辑,这是我试图避免的事情,并且没有关于如何从另一个页面调用 vue 组件的示例。

我得到的错误是...[Vue warn] 组件挂载失败:模板或渲染函数未定义

技术堆栈:

  • ASP.NET 核心 MVC (2.2)
  • Vue.js (2.6.10)
  • Vue-template-compiler (2.6.10)
  • Vue-loader (15.7.0) <-- 不确定我是否需要这个。
  • Axios(0.18.0)
  • 巴别塔/核心 (7.4.4)
  • Babel/polyfill (7.0.0) <--- 我的 UI 必须在 IE11 中工作并且我在我的 JS 中使用异步函数
  • 网页包 (4.26.0)

这是我的主要 _layout.cshtml

    <div id="main-app">
@RenderBody()
</div>
@section Scripts {
<script src="@Url.Content("~/js/mainVueApp.js")"></script>

}

这是我的 mainVueApp.js

 import Vue from 'vue';
import myComponent from 'testComponent';

new Vue({
el: 'main-app',
components: {
myComponent
// I also tried, thisIsMyComponent: myComponent. No luck with that either.
}
});

这是我的 _vueTestComponent.csthml

    @model TestViewModel 

<script type="text/x-template" id="my-test-component-id">
<div class="card">
<div class="card-header"> My Test Header </div>

<div class="card-body">
<table class="table">
<thead>
<tr>
<th class="pull-right">
Test Header
</th>
</tr>
</thead>

<tbody>
<tr v-if="isShowRow">
<th class="pull-right">
Test Header AGAIN
</th>

<tr class="pull-right">
@(Html.Kendo.CurrencyTextBoxFor(m =>
m.TestProperty).HtmlAttributes(new Dictionary<string, object>{
{"id", "test-text-box"},
{"class", "textbox-input"},
{"v-model", "testTextBoxValue"}
}).Deferred()
)
</tr>
</tr>
</tbody>
</table>
</div>
</div>
</script>
@section Scripts {
<script src="@Url.Content("~/js/testComponent.js")"></script>

}

这是我的 testComponent.js

import Vue from 'vue';
import axios from 'axios';

let testComponent = Vue.Component('my-component', {
template: 'my-test-component-id',
data: function() {
return {
testTextBoxValue : 'Test Value',
isShowRow: true
}

},
methods: {
// Nothing for now....
}
});

//I tried it without default, but it doesn't work either :(
export default {
testComponent
};

这是我调用组件TestViewForVue.cshtml的 View

<div class="row"> 
<div class="col-12">
@* Call the Component *@
@* Do I need to load the .cshtml where this component lives first, as a partial view or something? *@
<my-component> </my-component>
</div>
</div>

最佳答案

这会稍微改变您的结构(不使用脚本 x-模板语法),但它对我有用。如果可以,请尝试一下。

既然你想重用这个组件,在你的情况下全局注册它可能没问题。

此处使用模板文字,因此需要确保您为 IE11 正确转译

// testComponent.js

Vue.component("my-component", {
template: `
<div class="card">
<div class="card-header"> My Test Header </div>
</div>
`,
data () {
return {}
},
// ...
})

然后在您的 App_Start/BundleConfig.cs 中,为您的全局组件创建一个包,并在您的 _layout.cshtml 呈现脚本中呈现该包。

// BundleConfig.cs

public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/vuecomponents").Include(
"~/Scripts/VueComponents/testComponent.js"
));
}
// _layout.cshtml

@Scripts.Render("~/bundles/vuecomponents")

现在您应该能够包含 <my-component />在您应用的任何 .cshtml 页面上。

关于javascript - 如何在我的 .cshtml 页面中调用 Vue 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56294907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com