Skip to content

Instantly share code, notes, and snippets.

@silkentrance
Last active March 10, 2021 13:09
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silkentrance/9caa45481329f6ef911bd5eae5dbb7c3 to your computer and use it in GitHub Desktop.
Save silkentrance/9caa45481329f6ef911bd5eae5dbb7c3 to your computer and use it in GitHub Desktop.
vue-router typescript integration fails
import Vue from 'vue';
import Component from 'vue-class-component';
@Component({
name : 'component'
})
export default class MyComponent extends Vue {
beforeCreate() {
console.log('entering Bootstrap#beforeCreate');
}
created() {
console.log('entering Bootstrap#created');
}
activated() {
console.log('entering Bootstrap#activated');
}
beforeRouteEnter(to, from, next) {
console.log('entering Bootstrap#beforeRouteEnter');
next();
}
beforeRouteUpdate(to, from, next) {
console.log('entering Bootstrap#beforeRouteUpdate');
next();
}
}
<template>
<div>Guards are never called...</div>
</template>
<script lang="ts">
import MyComponent from './MyComponent';
export default MyComponent;
</script>
import Component from 'vue-class-component'
// Register the router hooks with their names
Component.registerHooks([
'beforeRouteEnter',
'beforeRouteLeave',
'beforeRouteUpdate' // for vue-router 2.2+
]);
import Vue from 'vue';
import VueRouter from 'vue-router';
import Component from 'vue-class-component'
import './register-hooks'
import MyComponent from './MyComponent.vue';
Vue.use(VueRouter)
export default new VueRouter({
mode : 'history',
routes : [
{ path: '/', component: MyComponent, meta : { title: 'Default Wiki Page'}},
]
});
@fechidal89
Copy link

fechidal89 commented Apr 21, 2020

Hi!, I have the same problem, but Is it required to have the component (MyComponent.ts) in another file?, and then import in "MyComponent.vue" file? I could not make it work.

@silkentrance
Copy link
Author

No, I just made use of the ability to separate the code from the actual template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment