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. apiCloud手动检测更新

    有时候需要给用户一个自主的权利,自主检测app是否是最新版本. 如何实现? 1.点击调用接口,检测是否有更新. 默认APICloud会自动检测版本更新,用户也可以在config.xml里配置autoU ...

  2. hosts 文件与 ipv6

    ipv6 的项目地址:ipv6-hosts 正如文档中所说,用于在大陆地区加快 Google.YouTube.Facebook.Wikipedia 等的访问:(twitter 不支持) 使用说明(wi ...

  3. 常见的C字符串处理函数的源代码

    #include <stdio.h> #include <assert.h> char *strcpy(char *strDest,const char *strSrc) // ...

  4. 动态调用WebService-获取天气

    string url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; string[] args ...

  5. BFS(广度优先搜索)

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  6. 洛谷 P1889 士兵站队

    P1889 士兵站队 题目描述 在一个划分成网格的操场上, n个士兵散乱地站在网格点上.由整数 坐标 (x,y) 表示.士兵们可以沿网格边上.下左右移动一步,但在同时刻任一网格点上只能有名士兵.按照军 ...

  7. ArcGIS Engine 9.3启动程序报错

    报错1: 没有注册类 (异常来自 HRESULT:0x80040154 解决办法: 由于是X64系统,将项目的生成目标该成X86就解决了. 报错2: 正试图在 OS 加载程序锁内执行托管代码.不要尝试 ...

  8. Android性能优化之提高ListView性能的技巧

    ListView优化一直是一个老生常谈的问题.无论是面试还是寻常的开发中,ListView永远不会被忽略掉,那么这篇文章我们来看看怎样最大化的优化ListView的性能. 1.在adapter中的ge ...

  9. Linux中 ps aux 命令

    $ ps aux USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND root 11 100.0 0.0 0 16 ?? RL 4Dec09 ...

  10. Swift vs C# Go OC

    Swift vs C#    mod=view&aid=21" target="_blank">http://www.swifthumb.com/porta ...