简体   繁体   English

Vue 使用来自 Vue() 的模板和脚本定义组件

[英]Vue define component with template and script from Vue()

I'm trying to define a component in my global Vue() init and have successfully defined the template for the component but cannot seem to define the actual class that is performing the work for the template.我正在尝试在我的全局 Vue() init 中定义一个组件,并已成功定义了该组件的模板,但似乎无法定义为模板执行工作的实际类。 I am using Vue with typescript.我正在使用带有打字稿的 Vue。

import ListClubsComponent from "./components/ts/list-club";

new Vue({
    el: "#app",
    components: {
        "list-clubs": {
            template: require("./components/clubs/list-clubs.html"),
            model: ListClubsComponent // This should be the class for the template
        }
    }
});

Instead of defining the template for your component at the global Vue() component level, try defining it inside of './components/ts/list-club':不要在全局 Vue() 组件级别为您的组件定义模板,而是尝试在 './components/ts/list-club' 中定义它:

var ListClubsComponent = {
    template : ...,
    data:...
    ...
}

Then import and register that entire component altogether at the global Vue() component:然后在全局 Vue() 组件中导入并注册整个组件:

import ListClubsComponent from "./components/ts/list-club";
new Vue({
    ...
    components : {
        'list-clubs' : ListClubsComponent
    }
    ...
})

This should also be easier to maintain since the template is grouped together with its functionality.这也应该更容易维护,因为模板与其功能组合在一起。

More info at https://v2.vuejs.org/v2/guide/components-registration.html#Local-Registration更多信息在https://v2.vuejs.org/v2/guide/components-registration.html#Local-Registration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM