首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在asp.net mvc局部视图中使用Vue组件(*.vue

如何在asp.net mvc局部视图中使用Vue组件(*.vue
EN

Stack Overflow用户
提问于 2018-05-10 20:14:03
回答 2查看 4.4K关注 0票数 2

我一直在努力跟上Vue.js的步伐,正在开发一个大量使用jQuery的asp.net MVC5Web应用程序,我想开始集成Vue并开始替换jQuery。

我现在已经花了几天的时间尝试将Vue集成到一个asp.net MVC5Web应用程序中,并在这个Best approach when replacing jQuery with VueJS 2 in multi-page existing .NET MVC application的帮助下,遵循了1_bug的答案。

因此,在我希望集成Vue的项目中,我正在考虑先在局部视图中使用组件,然后再处理其他视图。

所以简而言之,我的问题是,如何在局部视图中使用Vue组件(IE:*.vue文件)?

EN

回答 2

Stack Overflow用户

发布于 2020-11-28 18:10:17

我能够在我的ASP.NET核心项目中集成Vue v.2.6,而不需要JavaScript bundler使用部分视图。它在ASP.NET MVC项目中应该以同样的方式工作。

有关详细信息,请查看我对How do I set up ASP.NET Core + Vue.js?的回答或Github上的示例项目:ASP.NET Core + Vue.js

我还在Medium上一步一步地描述了Using Vue Components in ASP.NET Core

票数 1
EN

Stack Overflow用户

发布于 2018-07-26 15:26:51

我就是这么做的。.Vue文件只有在你使用vue-loader (webpack)的时候才是可能的,但是因为我们是一个遗留项目,所以这是不可能的

代码语言:javascript
复制
$("vue-category-gallery").each(function () {
    new Vue({
        el: this,
        data: () => ({
            action: "",
            categoryList: []
        }),
        beforeMount: function () {
            const actionAttribute = this.$el.attributes["data-action"];
            if (typeof actionAttribute !== "undefined" && actionAttribute !== null) {
                this.action = actionAttribute.value;
            } else {
                console.error("The data-attribute 'action' is missing for this component.");
            }
        },
        mounted: function () {
            if (this.action !== "") {
                CategoryService.getGalleryCategoryList(this.action).then(response => {
                    this.categoryList = response.data;
                });
            }
        },
        methods: {
            // none
        },
        template: `
            <div class="c-category-gallery" v-if="categoryList.length > 0">
                <div class="row">
                    <div class="col-md-3 col-sm-4" v-for="category in categoryList" :key="category.id">
                        <div class="c-category-gallery__item">
                            <img :src="category.imageUrl" :alt="category.name" class="img-responsive"></img>
                            <div class="c-category-gallery__item-content">
                                <h4>{{ category.name }}</h4>
                                <ul class="list-unstyled">
                                    <li v-for="subCategory in category.subCategoryList" :key="subCategory.id">
                                        <a :href="subCategory.categoryUrl" v-if="subCategory.showCategoryUrl">{{ subCategory.name }}</a>
                                    </li>
                                </ul>
                                <a :href="category.categoryUrl" v-if="category.showCategoryUrl">{{ category.detailName }}</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>`
    });
})

从HTML调用它的过程如下所示...

代码语言:javascript
复制
<vue-category-gallery 
    data-action="/api/categories/getcategorygallerylist?itemCount=4"
    v-cloak></vue-category-gallery>

我目前正在查看Vue.component("",...)但这仍处于测试阶段:)

编辑:

使用Vue.component("",...)

代码语言:javascript
复制
Vue.component("vue-person", {
    props: ["name"],
    data: function () {
        return {
            type: "none",
            age: 0
        }
    },
    template: `<div>
                   <p>My name is {{name}}.</p>
                   <p>I am {{type}} and age {{age}}.</p>
               </div>`
})

var vm = new Vue({
    el: "vue-components",
    components: [
        "vue-person"
    ]
})

$(".js-change-value").on("click", function () {
    vm.$refs.myPersonComponent.type = "human";
    vm.$refs.myPersonComponent.age = 38;
})

<vue-components>
    <vue-person name="Kevin" ref="myPersonComponent"></vue-person>
    <button type="button" class="js-change-value">Change value</button>
</vue-components>

我希望能够省略包装器,但是因为我已经在我的页面上使用了其他实例(new Vue()),我不能使用例如'#main‘( body元素上的id ),因为它与我的页面上已经有的其他全局实例冲突。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50272759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档