javascript - Vue 使用来自 Vue() 的模板和脚本定义组件
问题描述
我正在尝试在我的全局 Vue() init 中定义一个组件,并已成功定义了该组件的模板,但似乎无法定义为模板执行工作的实际类。我正在使用带有打字稿的 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://vuejs.org/v2/guide/components-registration.html#Local-Registration
推荐阅读
- swift - 当我运行我的程序时,模拟器只显示“人”部分,但不显示“视频”部分
- c# - SignalR 在集线器或控制器中实现 CRUD 方法
- tableau-api - Percentile_rank 计算作为计数计算的过滤器
- php - RSA 签名验证 - OpenSSL 与 phpseclib
- visual-studio - 用于 linux 的 windows 子系统上的外部可执行文件的 launch.vs.json 文件配置
- powershell - 通过 cmd.exe 调用的 powershell.exe 命令中的嵌套 cmd.exe 命令
- salesforce - Salesforce 批量 API 删除运算符
- postgresql - 将文本转换为 UTC 时间戳
- nginx - 在 Ubuntu 服务器上安装 Openresty
- javascript - DataTables 仅打印选定的选项