vuejs3 - Vue 3 组合 API 计算函数
问题描述
试图将我的代码切换到 Vue 3 附带的新组合 API,但我无法让它工作。
export default {
props: {
classProp: {type: String},
error: {type: String},
},
setup(){
// move to here (this is not working)
computed(() => {
const classObject = () => {
return ['form__control', this.classProp,
{
'form__invalid': this.error
}
]
}
})
},
computed: {
classObject: function () {
return ['form__control', this.classProp,
{
'form__invalid': this.error
}
]
}
},
}
解决方案
一起跳过“计算”,您需要使用“ref”或“reactive”。这些是模块:
<script>
import { ref } from 'vue'
setup(){
const whateverObject = ref({ prop: "whatever initial value" });
whateverObject.value.prop= "if you change something within setup you need to access it trough .value";
return { whateverObject } // expose it to the template by returning it
}
</script>
如果你想使用类,你可以像我自己的这个例子一样导入它们:
import { APIBroker } from '~/helpers/APIbroker'
const api = new APIBroker({})
现在“api”可以在 setup() 或任何地方使用
推荐阅读
- outlook - 将完整的电子邮件、正文和标题另存为 PDF
- javascript - 如何根据大小而不是时间从(网络)MediaRecorder 获取流数据?
- selenium-webdriver - Selenium webdriver 尝试访问弹出上下文
- java - JAXB/MOXY XmlAdapter 失败,可能的原因是适配器类名称不正确或设置了错误的加载器
- r - 如果变量的值小于 R 中的上一行 + X,则删除下一行
- node.js - Nodemon 在保存文件时给出 UnhandledPromiseRejectionWarning
- java - 广播接收器发送消息时未显示布局
- laravel - Laravel 8 S3 存储问题检索文件
- php - API函数OctoberCMS / Laravel
- javascript - 我们能否将错误作为来自 reactJS 中的 catch 块的 props 传递,并将它们显示在新的可重用组件中,其中 props 将类似于不同的错误