Get the current Vue Component Instance.
import { defineComponent, onMounted } from 'vue'
import { getCurrentInstance } from 'vue-composition-wrapper'
export default defineComponent({
setup() {
const app = getCurrentInstance()
onMounted(() => {
// You can access the properties and methods on the Vue instance from `app`
console.log(app)
})
},
})
Get the current Vue Component Instance context.
⚠️ Please usegetCurrentInstance
instead.
import { defineComponent, onMounted } from 'vue'
import { useContext } from 'vue-composition-wrapper'
export default defineComponent({
setup() {
const { app, store, route, router, query, params } = useContext()
onMounted(() => {
// You can access the properties and methods on the Vue instance from `app`
console.log(app)
})
},
})
⚠️ ⚠️ ⚠️ TIPS: For a friendly transition tovuex 4.x
,vue-router 4.x
, please do not usestore
,route
,router
,query
andparams
inuseContext
. Please useuseStore
,useRoute
anduseRouter
instead.
Access store
。
import { defineComponent, onMounted, computed } from 'vue'
import { useStore } from 'vue-composition-wrapper'
export default defineComponent({
setup() {
const store = useStore()
// state
const someStateInStore = computed(() => store.state.some.value)
// getters
const someGettersInStore = computed(() => store.getters['some/path'])
onMounted(() => {
// mutations
store.commit('some/SOME_MUTATION', 'foo')
// actions
store.dispatch('some/actions', 'bar')
})
return {
someStateInStore,
someGettersInStore,
}
},
})
IF you use Typescript, Use generic parameters to provide a type definitions for state:
import { defineComponent, onMounted, computed } from 'vue'
import { useStore } from 'vue-composition-wrapper'
import { RootState } from '@/store'
export default defineComponent({
setup() {
const store = useStore<RootState>()
// state
const someStateInStore = computed(() => store.state.some.value)
return {
someStateInStore,
}
},
})
Access current Router Object.
More details see useRouter
import { defineComponent, onMounted } from 'vue'
import { useRoute } from 'vue-composition-wrapper'
export default defineComponent({
setup() {
const router = useRouter()
const linkTo = (path) => {
router.push(path)
}
return {
linkTo,
}
},
})
Access the current routing information object.
More details see useRoute
import { defineComponent, onMounted } from 'vue'
import { useRoute } from 'vue-composition-wrapper'
export default defineComponent({
setup() {
const route = useRoute() // now, returns a non-ref (in v2, returns a ComputedRef)
onMounted(() => {
console.log(route)
})
},
})
Access the query
in the current routing information.
import { defineComponent, onMounted } from 'vue'
import { useRouteQuery } from 'vue-composition-wrapper'
export default defineComponent({
setup() {
const query = useRouteQuery() // returns a ComputedRef
onMounted(() => {
console.log(query.value)
})
},
})
Access the params
in the current routing information.
import { defineComponent, onMounted } from 'vue'
import { useRouteParams } from 'vue-composition-wrapper'
export default defineComponent({
setup() {
const params = useRouteParams() // returns a ComputedRef
onMounted(() => {
console.log(params.value)
})
},
})
⚠️ TIPS: Wherequery
andparams
are of typeComputedRef
.
Add a navigation guard that triggers whenever the component for the current location is about to be left. Similar to beforeRouteLeave
but can be used in any component. The guard is removed when the component is unmounted.
More details see onBeforeRouteLeave
Add a navigation guard that triggers whenever the current location is about to be updated. Similar to beforeRouteUpdate
but can be used in any component. The guard is removed when the component is unmounted.
More details see onBeforeRouteUpdate
More details see useLink
vue-composition-wrapper
will also provide some useful utility composition functions for you to use.
You can create custom helper
for any Vue instance property
.
You may wish to create a custom helper that "transforms" non-synthesis API properties into composition-ready properties. wrapProperty
enables you to do this easily, returning a computed property or a plain property as appropriate.
The second argument is a
boolean
indicating whether thehelper
function should return a computed property, defaults totrue
.
import { wrapProperty } from 'vue-composition-wrapper'
import { defineComponent } from 'vue'
// For example, for used with https://github.com/danielroe/typed-vuex
const useAccessor = wrapProperty('$accessor', false)
export default defineComponent({
setup() {
const accessor = useAccessor()
// You can now access a fully typed store accessor in your component
},
})