Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed context.root from every component #256

Merged
merged 6 commits into from
Jan 31, 2025
Merged

Conversation

onmax
Copy link
Member

@onmax onmax commented Jan 28, 2025

As we have said, 2025 is going to be the focus for moving away from Vue 2 to Vue 3. This task is tremendous, but we can always do small bits.

I have removed context.root calls and move it away from context to different places, so once we move to vue 3, we just need to change the import.

Here are the key changes

useRouter & useRoute

Before my PR

defineComponent({
	setup(props, context) {
		context.root.$router.push();
		context.root.$route.name;
	}
})

After my PR

import { useRouter } from "@/router";

defineComponent({
	setup() {
		const router = useRouter()
		router.push();
		router.currentRoute.name;
	}
})

How does useRouter work under-the-hood?

In app.vue we provide the context.root.router to the app.
Then inside useRouter we just use inject it.

After Vue 3

import { useRouter } from "@/router";

const router = useRouter()
router.push();
router.currentRoute.name;

useI18n

Before my PR

defineComponent({
	setup(props, context) {
		context.root.$i18n.t('Translate me');	
		context.root.$t('Translate me');
		context.root.locale;
		context.root.$tc('transalte with variable');
	}
})

After my PR

import { useI18n } from "@/lib/i18n";

defineComponent({
	setup(props, context) {
		const { $t, $tc, locale } = useI18n(); 
		$t('Translate me');	
		$tc('Translate me');
	}
})

How does useI18n work under-the-hood?

In app.vue we provide the context.root.$i18n to the app.
Then inside useI18n we just use inject it.

import { useI18n } from "@/lib/i18n";

const { $t, $tc, locale } = useI18n(); 
$t('Translate me');	
$tc('Translate me');

Next Tick

Before my PR

defineComponent({
	setup(props, context) {
		context.root.$nextTick();
	}
})

My PR

import { nextTick } from "@/lib/nextTick"; // My PR

defineComponent({
	setup(props, context) {
		nextTick();
	}
})

Vue 3!

Once we move to vue 3 will become (if we decide to not use auto-imports 😉)

import { nextTick } from "vue"; 

nextTick();

Missing context definitions

We still need to use context to define refs and emit, which I am not aware that we do a similar approach to ease the transition to vue 3. If you have an idea, please let me know.

@onmax onmax requested a review from sisou January 28, 2025 14:55
Copy link
Member

@sisou sisou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool!

@sisou sisou merged commit f1a764c into master Jan 31, 2025
1 check passed
@sisou sisou deleted the onmax/remove-context branch January 31, 2025 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants