[Vue + TS] Using Route events inside Vue
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- vue+ts搭建项目
Tip: 为了避免浪费您的时间,本文符合满足以下条件的同学借鉴参考 1.本文模版不适用于小型项目,两三个页面的也没必要用vue2.对typescript.vue全家桶能够掌握和运用 此次项目模版主要涉 ...
- [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 ...
- 【vue&ts开发】Vue 3.0前的 TypeScript 最佳入门实践
1.使用官方脚手架构建 新的 VueCLI工具允许开发者 使用 TypeScript 集成环境 创建新项目. 只需运行 vue createmy-app. 然后,命令行会要求选择预设.使用箭头键选择 ...
- 深入使用Vue + TS
深入使用TS 支持 render jsx 写法 这里一共分两步 首先得先让 vue 支持 jsx 写法 再让 vue 中的 ts 支持 jsx 写法 让 vue 支持 jsx 按照官方做法,安装Bab ...
- 问题记录---关于posiition脱离文档流及vue中this.$route信息
1.关于position:fixed会脱离文档流 简单例子: 原型有三个div盒子: 将剥box1设置为position:fixed后 从上图可以看出:box1脱离了文档流,且层级显示优先于正常文档, ...
随机推荐
- 罗列几个Android插件化开发框架
携程插件化框架 ACDD插件化框架 360插件化框架 Android-Plugin-Framework DL APK动态加载框架 部分框架对比 DynamicLoadApk 迁移成本很重:需要使用『t ...
- css3 边框阴影效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 内联函数(Inline Functions)
影响性能的一个重要因素是内联技巧.内联函数也可称为内嵌函数. 在C++中,函数调用需要建立栈环境,进行参数复制,保护调用现场,返回时,还要进行返回值复制,恢复调用现场.这些工作都是与完成特定任务的操作 ...
- windows 手动添加服务
windows 手动添加服务方法一:修改注册表 在注册表编辑器,展开分支"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services" ...
- C/C++(C++类与对象)
构造器(constructor) 1.与类名相同,无返回,被系统生成对象时自动调用,用于初始化. 2.可以有参数,构造器的重载,有默认参数.重载和默认参数不能同时出现,但是一定要包含标配(无参数的构造 ...
- Mysql source导入.sql文件深坑!
刚刚接手一个项目,给老系统加功能.把数据库考出来一个.sql文件就170多M. 使用mysql命令行source 我的.sql文件. 导了一宿都没导完,然后发现里面的数据怎么是乱码呢.. 崩溃额,在排 ...
- 学习《Python数据科学手册》高清中文PDF+高清英文PDF+代码
如果有一定的数据分析与机器学习理论与实践基础,<Python数据科学手册>这本书是绝佳选择. 是对以数据深度需求为中心的科学.研究以及针对计算和统计方法的参考书.很友好实用,结构很清晰.但 ...
- CCF模拟题 最优配餐
最优配餐 时间限制: 1.0s 内存限制: 256.0MB 问题描述 栋栋最近开了一家餐饮连锁店,提供外卖服务.随着连锁店越来越多,怎么合理的给客户送餐成为了一个急需解决的问题. 栋栋的连锁店所在 ...
- SSL通关之代码演示样例(四)
实际开发过程中,server端是不须要多加代码处理的,由于ssl验证过程是由server(tomcat.nginx等)完毕的. 这段代码也是參考了网上的: 新建一个web项目,项目结构和须要引入的ja ...
- vim 计算器寄存器使用
我们可能会在vim的使用中,碰到下面的情况 当我正在写一周预算的时候,我想计算下每天我买菜花2.7,每天买两顿,周死晚上出去吃,周六额外买1.5斤14.8一斤的猪肉... 这时候你打算怎么办呢,是不是 ...