问题已解决,请通读问题,了解如何逐步解决问题
我阅读了有关组合 API(https://vue-composition-api-rfc.netlify.com/#api-introduction)的信息,并尝试转换我现有的基于 vuejs 中 Option api 上的打字稿的代码,但我未能让它工作,有点困惑组合 API 是如何工作的。
我的工作代码如下:
<script lang="ts">
import Vue from "vue";
import {inputFields} from "@/mixins/inputField/inputField";
import VuePersianDatetimePicker from "vue-persian-datetime-picker"
import {util_timestampz_formatter, util_timestampz_now} from "@/utils/commonUtility";
import {InputFieldHelper} from "@/mixins/Helpers/FormGeneratorInputFieldHelper/InputField.Helper.mixin";
export default Vue.extend({
name: "DateTimePicker",
props: ['instance', "value", "formSection", "isEnable"],
components: {
datePicker: VuePersianDatetimePicker
},
data() {
return {
inputField: new InputFieldHelper(this.value, this.instance, this.formSection, this.isEnable)
}
},
mounted() {
//@ts-ignore
this.instance.min = util_timestampz_now();
},
methods: {
updateValue() {
let new_data = this.inputField.updateValue();
new_data ? this.$emit("updateValue", new_data) : "";
},
updateDateTime(time: string) {
//@ts-ignore
this.inputField.inputValue = util_timestampz_formatter(this.inputField.inputValue);
//@ts-ignore
this.updateValue();
},
}
});
</script>
组合 api 中的转换版本低于未正常运行的版本:
<script lang="ts">
import Vue from "vue";
import {reactive, createComponent, onMounted} from "@vue/composition-api";
import VuePersianDatetimePicker from "vue-persian-datetime-picker"
import {util_timestampz_formatter, util_timestampz_now} from "@/utils/commonUtility";
import {InputFieldHelper} from "@/mixins/Helpers/FormGeneratorInputFieldHelper/InputField.Helper.mixin";
import {FormGeneratorInputField} from "@/types";
export default createComponent({
name: "DateTimePicker",
props: {
instance,
value,
formSection,
isEnable,
},
components: {
datePicker: VuePersianDatetimePicker
},
setup(props, context) {
const inputField = reactive(new InputFieldHelper(props.value, props.instance, props.formSection, props.isEnable));
onMounted(() => {
props.instance.min = util_timestampz_now();
});
const updateValue = () => {
let new_data = inputField.updateValue();
new_data ? context.$emit("updateValue", new_data) : "";
};
const updateDateTime = (time: string) => {
inputField.inputValue = util_timestampz_formatter(inputField.inputValue);
updateValue();
};
return {
inputField, updateValue, updateDateTime
}
}
});
</script>
其中 webstorm 显示 3 个错误:
有没有办法正确解决这个问题?
.....................
更新1:
我在使用道具时犯了错误(我以为我是新的 vue,它不需要类型,它会自动从父组件的 typescript 中获取它)。多亏了古问题,问题已经解决并得到解决。
感谢https://frontendsociety.com/using-a-typescript-interfaces-and-types-as-a-prop-type-in -vuejs-508ab3f83480 ,您可以将创建的类型接口作为如下函数传递:
但仍有一些问题需要解决,如下所示:
1.当我删除反应性时,反应性似乎无法正常工作,并且在模板中显示“未解析的变量或缺少导入语句”
…………………………………………………………………………………………
update2分享道具和使用合成功能:
对我来说,在将基于选项的 api 转换为组合 api 之后,它似乎比新的更好,这要归功于古和 Maximilian shrwazmuller(我强烈建议观看这个免费视频: https: //www.youtube.com/watch ?v=V-xK3sbc7xI),谷歌搜索与这个https://vue-composition-api-rfc.netlify.com/api.html#template-refs我重新编写代码如下:
<script lang="ts">
import {createComponent, onMounted} from "@vue/composition-api";
import VuePersianDatetimePicker from "vue-persian-datetime-picker"
import {util_timestampz_formatter, util_timestampz_now} from "@/utils/commonUtility";
import {
useInputFieldComposition,
inputFieldProps
} from "@/mixins/Helpers/FormGeneratorInputFieldHelper/inputField.composition";
import {ComponentPropsOptions} from "@vue/composition-api/dist/component/componentProps";
export default createComponent({
name: "DateTimePicker",
props: inputFieldProps as ComponentPropsOptions,
components: {
datePicker: VuePersianDatetimePicker
},
setup(props, context) {
const {inputField, updateValue} = useInputFieldComposition(props, context);
onMounted(() => {
props.instance.min = util_timestampz_now();
});
const updateDateTime = (time: string) => {
inputField.inputValue = util_timestampz_formatter(inputField.inputValue);
updateValue();
};
return {
inputField, updateValue, updateDateTime
}
}
});
</script>
如您所见,它非常简洁,因为将在组件之间共享的所有内容都在另一个文件中(名为:inputFiled.compositons),如下所示:
//inputField.composition.ts
import {PropType, reactive} from "@vue/composition-api";
import {FormGeneratorInputField} from "@/types";
import {InputFieldHelper} from "@/mixins/Helpers/FormGeneratorInputFieldHelper/InputField.Helper.mixin";
export const inputFieldProps = {
instance: {
type: Object as PropType<FormGeneratorInputField>,
required: true
},
value: {
type: Object,
required: true
},
formSection: {
type: String,
required: true
},
isEnable: {
type: Boolean,
required: true
},
};
export const useInputFieldComposition = (props: any, context: any) => {
let inputField = reactive(new InputFieldHelper(props.value, props.instance, props.formSection, props.isEnable));
const updateValue = (): any => {
let new_data = inputField.passInfoMixedValue();
new_data ? context.emit("updateValue", new_data) : "";
return new_data;
};
return {
props, inputField, updateValue
}
正如您所看到的,通过此功能,我们可以充分发挥打字稿的潜力。
最后我应该告诉你,如果你遵循路径并使用 vue-composition api,你的项目将完美运行,但 webstorm 会向你显示一些关于模板中特定代码的警告,比如未解析的变量。这些新的语法支持正在开发中,您可以在本期https://youtrack.jetbrains.com/issue/WEB-41565中看到, 所以不要担心并使用它。