vue-router introduces new hooks into the component. In this lesson we’ll show you how to use these new hooks in your class based Vue components in TypeScript. We’ll also go over how we can create and use routes in Vue.

Default component:

<template>
<div class="hello">
<h1>{{ message }}</h1>
<button @click="clicked">Click</button>
<router-link to="/hello-ts">Hello Ts</router-link>
</div>
</template> <script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component' @Component({})
export default class Hello extends Vue {
message: string = 'Welcome to Your Vue.js App' get fullMessage() {
return `${this.message} from Typescript`
} created() {
console.log('created');
} beforeRouteEnter(to, from, next) {
console.log("Hello: beforeRouteEnter")
next()
} beforeRouteLeave(to, from, next) {
console.log("Hello: beforeRouteLeave")
next()
} clicked() {
console.log('clicked');
}
}

Create a second component:

<template>
<div>
<h1>Hello Ts</h1>
<router-link to='/'>Hello Vue</router-link>
</div>
</template> <script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import Route from 'vue-router'; @Component({})
export default class HelloTs extends Vue{
created() {
console.log("Hello TS created")
} beforeRouteEnter(to: Route, from: Route, next: Function) {
console.log("beforeRouteEnter")
next();
} beforeRouteLeave(to: Route, from: Route, next: Function) {
console.log("beforeRouteLeave")
next();
}
}
</script>

Checkout Doc

There are tow route events, "beforeRouteEnter" and "beforeRouteLeave", each lifecycle hooks accepts three params "to, from , next", just like middlewares in nodejs, we need to call "next()" to make everything works.

Also we need to register the route link before using Vue.

hooks.ts:

import Component from 'vue-class-component'

Component.registerHooks([
'beforeRouteEnter',
'beforeRouteLeave'
])

main.ts:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias. import './hooks' import Vue from 'vue'
import App from './App'
import router from './router' Vue.config.productionTip = false /* eslint-disable no-new */
new Vue({
el: '#app',
router,
template: '<App/>',
components: { App }
})

[Vue + TS] Using Route events inside Vue的更多相关文章

  1. [Vue + TS] Watch for Changes in Vue Using the @Watch Decorator with TypeScript

    Vue watchers allow to perform async updates as a side effect of a property change. This lesson shows ...

  2. [Vue +TS] Use Two-Way Binding in Vue Using @Model Decorator with TypeScript

    Vue models, v-model, allow us to use two-way data binding, which is useful in some cases such as for ...

  3. [Vue + TS] Use Dependency Injection in Vue Using @Inject and @Provide Decorators with TypeScript

    Vue 2.2 introduced a simple dependency injection system, allowing you to use provide and inject in y ...

  4. [Vue + TS] Write a Vue Component as a Class in TypeScript

    Starter app: https://github.com/alexjoverm/Vue-Typescript-Starter Writing Vue components as plain ob ...

  5. vue+ts搭建项目

    Tip: 为了避免浪费您的时间,本文符合满足以下条件的同学借鉴参考 1.本文模版不适用于小型项目,两三个页面的也没必要用vue2.对typescript.vue全家桶能够掌握和运用 此次项目模版主要涉 ...

  6. [Vue + TS] Use Properties in Vue Components Using @Prop Decorator with TypeScript

    With properties we can follow a one-way parent→child flow communication between components. This les ...

  7. 【vue&ts开发】Vue 3.0前的 TypeScript 最佳入门实践

    1.使用官方脚手架构建 新的 VueCLI工具允许开发者 使用 TypeScript 集成环境 创建新项目. 只需运行 vue createmy-app. 然后,命令行会要求选择预设.使用箭头键选择  ...

  8. 深入使用Vue + TS

    深入使用TS 支持 render jsx 写法 这里一共分两步 首先得先让 vue 支持 jsx 写法 再让 vue 中的 ts 支持 jsx 写法 让 vue 支持 jsx 按照官方做法,安装Bab ...

  9. 问题记录---关于posiition脱离文档流及vue中this.$route信息

    1.关于position:fixed会脱离文档流 简单例子: 原型有三个div盒子: 将剥box1设置为position:fixed后 从上图可以看出:box1脱离了文档流,且层级显示优先于正常文档, ...

随机推荐

  1. HAProxy高可用配置视频教程

    HAProxy提供高可用性.负载均衡等,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理.HAProxy运行在当前的硬件上, ...

  2. noip 2018 day1 T1 铺设道路 贪心

    Code: #include<cstdio> using namespace std; int main() { int last=0,ans=0; int n;scanf("% ...

  3. 关于Echarts表格插件的使用

    <template> <div :style="{height:height,width:width}"></div> </templat ...

  4. 【APP测试】APP弱网环境测试

    方法一:利用抓包工具 1.利用fiddler通过代理连接上手机之后,进入Fiddler->Rules->Customize Rules,点击弹出的CustomRules.js文件,找到m_ ...

  5. Struts2的token标签

    “token标签的实现原理是在表单中增加一个隐藏域,每次加载该页面时,该隐藏域的值都不相同.而TokenInterceptor拦截器则拦截所有用户请求,如果两次请求时该token对应隐藏域的值相同(前 ...

  6. hdu 5312 Sequence(数学推导——三角形数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others)  ...

  7. js插件---datatables如何使用

    js插件---datatables如何使用 一.总结 一句话总结:a.引入css和js(不要忘记css):b.js代码启动插件(里面可以用参数控制各种功能) 1.dataTables如何显示控制行(比 ...

  8. Logistic Regression and Newton's Method

    Data For this exercise, suppose that a high school has a dataset representing 40 students who were a ...

  9. dp水题

    hdu 2084: #include <stdio.h> #include <iostream> #include <string.h> using namespa ...

  10. HDU——T 2594 Simpsons’ Hidden Talents

    http://acm.hdu.edu.cn/showproblem.php?pid=2594 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...