I am new to vue js, i was from angular...
From what i read, vue js has two way of writing, options API vs composition API.
Options API has something like:
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
name: "App",
data() {
return {
count: 0
};
}
});
</script>
Where Composition API has the format of:
<script lang="ts">
import { ref, defineComponent } from "vue";
export default defineComponent({
name: "App",
setup: () => {
const count = ref(0);
return { count };
},
});
</script>
But when i read the existing code base, i can't really know which type of codebase is this? What is the difference?
We use micro frontend as we are small part of entire webpage.
<script lang="ts">
import { Component, Vue } from 'XXXXX';
import { BannerType, showBanner } XXXXX';
import { itemService} from 'XXXXX';
#Component
export default class UploadItemModal extends Vue {
#Ref('form')
readonly form!: InstanceType<typeof ValidationObserver>;
private isUploading = false;
private showUploadError = '';
private showProgressbar = false;
uploadFieldName = '';
fileCount = 0;
fileSize = '';
selectedFile: File = new File([], '');
uploading = {
loaded: 0,
total: 1,
};
private closeDialog(): void {
//implementation hidden
}
private clearData(): void {
//implementation hidden
}
</script>
Related
I want to format my date-time outside the component.
function transformDatetime(config, value) {
if (value) {
return $d(new Date(value), config);
}
return $t('-');
}
I'm trying to get $t from the App instance. But it only works in the context of the component, just like useI18n.
import { getCurrentInstance } from 'vue'
export default function useGlobal() {
const instance = getCurrentInstance()
if (!instance) return
return instance.appContext.config.globalProperties
}
I found a solution. Just import your i18n into a file outside the application and use i18n.global.t
import { createI18n } from "vue-i18n"
export const i18n = createI18n({
legacy: false,
locale: 'ja',
fallbackLocale: 'en',
messages,
})
import { i18n } from '#/i18n'
const { t: $t, d: $d, n: $n } = i18n.global
const expample = $t('some-text')
I think your method should also be a composable so you can simply use the method useI18n() inside.
Something like
use-transform-datetime.ts
import { useI18n } from 'vue-i18n'
export const useTransformDatetime = () => {
const { t } = useI18n()
const transformDatetime = (config, value) => {
if (value) {
return $d(new Date(value), config)
}
return t('-')
}
return {
transformDatetime
}
}
then simply use it in your vue file who need it
<script setup>
import { useTransformDatetime } from 'path-to-your-composables/use-transform-datetime'
const { transformDatetime } = useTransformDatetime()
console.log(transformDatetime(config, 'value'))
</script>
This way you can even add your others transform methods in the same composable
Try to use of the useI18n composable function from vue-i18n to get the t method instead of $t:
<script setup>
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
console.log(t('greeting'))
</script>
<template>
<div>
<h1>Vuex Typescript Test</h1>
<button #click="handleLogin">click</button>
</div>
</template>
<script lang="ts">
import { defineComponent } from '#vue/composition-api'
export default defineComponent({
setup() {
return {
handleLogin() {
// something....
},
}
},
})
</script>
#vue/composition-api do not apply useStore
I want to use store in setup function.
You should be able to access the useStore composable in the setup function according to the documentation of Vuex.
Your script section will look like this:
import { defineComponent } from '#vue/composition-api';
import { useStore } from 'vuex';
export default defineComponent({
setup() {
return {
const store = useStore();
return {
handleLogin {
store.dispatch('auth/login');
},
};
}
},
});
The proper way to structure the content of setup would be to move the handleLogin as a separate const and expose the constant in the return, in order to keep the return section more readable like this:
setup() {
const store = useStore();
const handleLogin = () => {
store.dispatch('auth/login');
};
return {
handleLogin,
}
}
Is there a way to get access to $vuetify (and any other added global) in the setup function?
Is there a way for composables to access it?
...
setup() {
const { isDesktop } = $vuetify.breakpoints.mdAndUp; // <=== how to get $vuetify
return { isDesktop };
},
Composable to get vuetify instance:
// useVuetify.ts
import { getCurrentInstance } from 'vue'
export function useVuetify() {
const instance = getCurrentInstance()
if (!instance) {
throw new Error(`useVuetify should be called in setup().`)
}
return instance.proxy.$vuetify
}
Import it in your component:
<!-- MyComponent.vue -->
<script lang="ts">
import { useVuetify } from './useVuetify'
import { computed } from 'vue'
/*...*/
setup() {
const vuetify = useVuetify()
const isDesktop = computed(()=>vuetify.breakpoints.mdAndUp)
return { isDesktop }
},
/*...*/
</script>
If you are using Vue <= 2.6.14 + #vue/composition-api instead of Vue 2.7, replace 'vue' with '#vue/composition-api'
As #Lagamura mentioned in the comments, this can be achieved with Vuetify 3 using useDisplay().
E.g.
const display = useDisplay();
console.log(display.mobile.value);
I'm working on a project that's both mobile and PC,I need to estimate the mobile terminal or PC terminal。
flexible.js
import { computed } from 'vue'
import { PC_DEVICE_WIDTH } from '../constants'
import { useWindowSize } from '#vueuse/core/index'
const { width } = useWindowSize()
// 判断当前是否为移动设备,判断依据屏幕宽度是否小于一个指定宽度(1280)
export const isMobileTerminal = computed(() => {
return width.value < PC_DEVICE_WIDTH
})
and the navigation/index.vue code is
<template>
<mobile-navigation v-if="isMobileTerminal"></mobile-navigation>
</template>
<script>
import { isMobileTerminal } from '../../../../utils/flexible'
import mobileNavigation from './mobile/index.vue'
export default {
name: 'index',
components: {
mobileNavigation
}
}
</script>
<style lang="scss" scoped></style>
My project catalog is shown below
isMobileTerminal is only imported in your component. It also needs to be made available to the template by declaring it in your component definition.
Returning it from the setup() hook is one way to do that:
<script>
import { isMobileTerminal } from '../../../../utils/flexible'
export default {
setup() {
return {
isMobileTerminal
}
}
}
</script>
What do I want to achieve:
The form is only loaded if processingData is available.
Currently the form is not loading because processingData is not available
The problem I am currently running into is that when I load the application (in the browser, or possibly f5), no data is visible within processingData. If I modify the application and rebuild again in the editor, I see the correct value. I can't get out anymore..
Create.vue (FILE)
<template>
<FormModal v-if="processingData" :processingData="processingData" #gateway="saveForm" />
</template>
<style lang="scss" scoped>
.el-input--suffix .el-input__inner {
background-color: #f5f8fa;
}
.el-input__inner {
background-color: #f5f8fa;
}
</style>
<script lang="ts">
import { defineComponent, computed, ref, onMounted } from "vue";
import { useStore } from "vuex";
import FormModal from '#/components/forms/FormModal.vue';
export default defineComponent({
name: "",
components: {
FormModal,
},
setup() {
const form = ref({});
const componentName = ref('SalesTable');
const processingData = ref(null)
const store = useStore();
const components1 = computed(() => {
return store.getters.getComponents;
});
function loadComponent() {
console.log(store.getters.getComponents) // Shows nothing (Only after I update the code and rebuild is, it shows data)
console.log(components1.value) // Shows nothing (Only after I update the code and rebuild is, it shows data)
const components = components1.value;
let i;
for (i = 0; i < components.length; i++) {
if (components[i].name == componentName.value) {
processingData.value = components[i];
}
}
}
onMounted(() => {
loadComponent()
})
return {
form,
processingData,
components1,
};
}
});
</script>