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

标签 javascript typescript vue.js vuejs2 vue-component

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

最佳答案

与其在全局 Vue() 组件级别定义组件模板,不如尝试在 './components/ts/list-club' 中定义它:

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

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

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

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

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

关于javascript - Vue 使用来自 Vue() 的模板和脚本定义组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59182893/

相关文章:

javascript - 如何从 jsonp 响应中获取值并将该值设置为其他函数中的其他变量?

javascript - jQuery遍历最接近类的<div>

javascript - 相同的计算函数,不同的结果取决于 .load() 和 .resize() 或 .scroll()

javascript - native .click() 在 Chrome 57 中不触发 Blob 下载

javascript - 如何重写构造函数?

javascript - 使用 JSweet 将 Java 模块移植到 JS

angular - Typescript 通过包含特殊字符的键从数组中删除项目

typescript - Vue和 typescript : How to avoid TS error when accessing child component's methods

typescript - Vue Component Props 上的数组类型破坏了 Linter

vue.js - 使用 Vue router-link 进行导航,仅更改查询参数,而不更改路径