【问题标题】:Vue define component with template and script from Vue()Vue 使用来自 Vue() 的模板和脚本定义组件
【发布时间】:2020-03-29 15:52:42
【问题描述】:

我正在尝试在我的全局 Vue() 初始化中定义一个组件,并已成功定义了该组件的模板,但似乎无法定义为模板执行工作的实际类。我正在使用带有打字稿的 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
        }
    }
});

【问题讨论】:

    标签: javascript typescript vue.js vuejs2 vue-component


    【解决方案1】:

    不要在全局 Vue() 组件级别为您的组件定义模板,而是尝试在 './components/ts/list-club' 中定义它:

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

    然后在全局 Vue() 组件中导入并注册整个组件:

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

    这也应该更容易维护,因为模板与其功能组合在一起。

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

    【讨论】:

    • 效果很好.. 现在如果我想为其添加 SCSS 样式,我该怎么做?
    • 太棒了!如果您想将所有 (S)CSS、JS 和 HTML 组合到一个文件中用于单个组件,Vue 有一个很棒的框架,称为 Vue 单文件组件:vuejs.org/v2/guide/single-file-components.html 在您的项目开始时开始使用它很棒越来越严重
    猜你喜欢
    • 2021-07-26
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 2020-08-26
    • 2019-01-29
    • 1970-01-01
    • 2019-08-29
    相关资源
    最近更新 更多