我偶然发现了一些与打字稿和Vue3有关的问题。我想我在这里使用打字本是错的。
我用Vue的复合API创建了一家商店。
import {computed, reactive} from "vue";
const state = reactive({
accessToken: undefined,
user: {},
})
const isAuthenticated = computed(() => {
return state.accessToken !== undefined
})
export default {
state: readonly(state),
isAuthenticated
}为它编写类型/接口:
import {ComputedRef} from "vue";
export interface UserStore{
state: Readonly<any>;
isAuthenticated: ComputedRef<boolean>;
}但是当我现在想在我的vue组件中使用它时。
例如,就像这样:
<h1 v-if="userStore.isAuthenticated">is true</h1>即使它显然是假的,它也会返回true。
我通过注射给商店注射:
setup(){
return {
userStore: inject('userStore') as UserStore
}
}当我想返回计算()字符串时,也会发生类似的问题。当我在模板中使用它时,它是用引号包装的。
这里有什么问题?
#编辑我在main.ts中提供了main.ts
/* Stores */
import UserStore from './store/user-store'
const app = createApp(App)
.use(IonicVue, {
mode: 'ios'
})
.use(router)
.provide('userStore',UserStore);
router.isReady().then(() => {
app.mount('#app');
});发布于 2021-12-02 14:10:56
ref是一个对象,所以userStore.isAuthenticated表达式总是真实的。
当从安装函数返回引用(包括计算值)时,会在模板中自动展开它们。它应该是:
const userStore = inject('userStore') as UserStore
const { isAuthenticated } = userStore;
return {
isAuthenticated
}否则,需要手动打开ref:
<h1 v-if="userStore.isAuthenticated.value">is true</h1>发布于 2021-12-02 14:02:41
如果要在类型记录中创建计算属性,则需要添加一个get infront方法。
get isAuthenticated () {
return state.accessToken !== undefined
)创建类型接口
interface recordTypes {
pDate: Date;
}以下是完整的示例
<template>
<component
v-if="componentName != ''"
v-bind:is="componentName"
v-on:updateConfirmationStatus="updateConfirmation"
>
</component>
</template>
<script lang="ts">
import { Vue, Options } from "vue-class-component";
import { useStore, ActionTypes } from "../store";
import Toaster from "../helpers/Toaster";
import { reactive } from "vue";
import PatientConsultationService from "../service/PatientConsultationService";
import Confirmation from "../components/Confirmation.vue";
import { required } from "@vuelidate/validators";
import useVuelidate from "@vuelidate/core";
import { camelCase } from "lodash";
import moment from "moment";
interface recordTypes {
pDate: Date;
}
@Options({
components: {
Confirmation
}
})
export default class Assessments extends Vue {
private recordList: recordTypes[] = [];
private recordID = 0;
private pService;
private isCollapsed = true;
private submitted = false;
private toast;
private componentName = "";
private vuexStore = useStore();
private state = reactive({
weight: 0,
height: 0,
o2: 0,
respiration: 0,
temperature: 0,
pressure: 0,
pulse: 0
});
private validationRules = {
weight: {
required
},
height: {
required
},
o2: {
required
},
respiration: {
required
},
temperature: {
required
},
pressure: {
required
},
pulse: {
required
}
};
private v$ = useVuelidate(this.validationRules, this.state);
created() {
this.pService = new PatientConsultationService();
this.toast = new Toaster();
}
mounted() {
this.loadList();
}
get patientID() {
return this.vuexStore.getters.getReceiptID;
}
saveItem(isFormValid) {
this.submitted = true;
if (isFormValid) {
this.pService
.saveAssessment(this.state, this.patientID)
.then(res => {
this.clearItems();
this.toast.handleResponse(res);
this.loadList();
});
}
}
clearItems() {
this.state.weight = 0;
this.state.height = 0;
this.state.o2 = 0;
this.state.respiration = 0;
this.state.temperature = 0;
this.state.pressure = 0;
this.state.pulse = 0;
}
loadList() {
this.pService.getAssessment(this.patientID).then(data => {
const res = this.camelizeKeys(data);
this.recordList = res.records;
});
}
camelizeKeys = obj => {
if (Array.isArray(obj)) {
return obj.map(v => this.camelizeKeys(v));
} else if (obj !== null && obj.constructor === Object) {
return Object.keys(obj).reduce(
(result, key) => ({
...result,
[camelCase(key)]: this.camelizeKeys(obj[key])
}),
{}
);
}
return obj;
};
get sortedRecords() {
let sortedList = {};
this.recordList.forEach(e => {
const date = String(e.pDate);
if (sortedList[date]) {
sortedList[date].push(e);
} else {
sortedList[date] = [e];
}
});
return sortedList;
}
formatDate(d) {
const t = moment().format("YYYY-MM-DD");
let fd = "";
if (d == t) {
fd = "Today " + moment(new Date(d)).format("DD-MMM-YYYY");
this.isCollapsed = false;
} else {
fd = moment(new Date(d)).format("DD-MMM-YYYY");
this.isCollapsed = true;
}
return fd;
}
deleteItem(id) {
this.vuexStore.dispatch(
ActionTypes.GET_RECEIPT_TITLE,
"Are you sure to delete this"
);
this.componentName = "Confirmation";
this.recordID = id;
}
updateConfirmation(b) {
this.componentName = "";
if (b.result) {
this.pService.deleteAssessment(this.recordID).then(res => {
this.toast.handleResponse(res);
this.loadList();
});
}
}
}
</script>https://stackoverflow.com/questions/70200493
复制相似问题