[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>
        <h3>HelloTs</h3>
        <router-link to="/">Hello</router-link>
    </div>
</template>
<script lang="ts">

import Vue from ‘vue‘
import Component from ‘vue-class-component‘

@Component({})
export default class HelloTs extends Vue {
    created() {
        console.log("HelloTs created!")
    }
    beforeRouteEnter(to, from, next) {
        console.log("HelloTs: beforeRouteEnter")
        next()
    }

    beforeRouteLeave(to, from, next) {
        console.log("HelloTs: beforeRouteLeave")
        next()
    }
}
</script>

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 }
})
时间: 2024-12-28 00:11:36

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

[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 your component options. This lesson shows you how to use them using the @Inject and @Provide decorators in tandem! When you want to provide some servic

[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 forms. This lesson shows how to use it by creating a custom checkbox component using the @Model decorator in TypeScript. Checkbox: <template> <div>

vue+ts搭建项目

Tip: 为了避免浪费您的时间,本文符合满足以下条件的同学借鉴参考 1.本文模版不适用于小型项目,两三个页面的也没必要用vue2.对typescript.vue全家桶能够掌握和运用 此次项目模版主要涉及的技术框架: vue2.5 vuex3.0 vue-router3.0 axios typescript3.2 Tip: 由于vue-cli3.0帮我们简化了webpack的配置,我们只需要在根目录下的vue.config.js文件的chainWebpack进行配置即可. 接下来进入正题(前提是你

[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 lesson shows you how you can pass down properties to class based Vue components by using the @Prop decorator from vue-property-decorator. We’ll also see

基于VUE+TS中引用ECharts的中国地图和世界地图密度表

首先先附上官网 http://echarts.baidu.com/option.html#series-line.label 以及密度表对应geo配置文档 http://echarts.baidu.com/option.html#geo 以下仅是个人在开发中逐步摸索出来的.demo目前没出问题.如果有错误地方请留言指出  (若转载请标注出处) 直接上效果图,对应代码在效果图下面 安装: 1. npm install echarts --save2. npm install --save @typ

深入使用Vue + TS

深入使用TS 支持 render jsx 写法 这里一共分两步 首先得先让 vue 支持 jsx 写法 再让 vue 中的 ts 支持 jsx 写法 让 vue 支持 jsx 按照官方做法,安装Babel 插件 安装依赖 npm install babel-plugin-syntax-jsx babel-plugin-transform-vue-jsx babel-helper-vue-jsx-merge-props babel-preset-es2015 --save-dev 在.babelr

vue+ts搭建工程

1.安装必要的插件 npm i vue-class-component vue-property-decorator --save npm i ts-loader typescript tslint tslint-loader tslint-config-standard --save-dev 2.配置webpack修改webpack.base.conf.js文件 entry: { app: './src/main.ts' // main.js->main.ts }, resolve: { ex

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

1.关于position:fixed会脱离文档流 简单例子: 原型有三个div盒子: 将剥box1设置为position:fixed后 从上图可以看出:box1脱离了文档流,且层级显示优先于正常文档,他们之间的层级关系是:z-index负数<正常<float(和文档同级)<position<z-index正数 要使得box2及之下显示正常:要使fixed定位的left和top为0,固定到窗口顶部,是box2的margin-top设置为box1的height <!DOCTYPE

vue项目搭建和开发流程 vue项目配置ElementUI、jQuery和Bootstrap环境

目录 一.VUE项目的搭建 1. 环境搭建 2. 项目的创建和启动 二. 开发项目 1. 配置vue项目启动功能 2. 开发vue项目 (1)项目文件的作用 (2)vue项目开发流程 (3)vue项目的请求生命周期 三.vue项目中的功能 1. 路由相关的标签和方法 2. 路由配置 (1)无路由传参的路由配置方法 (2)路由传参的路由配置方法 3. 页面的跳转功能 (1)标签跳转 (2)逻辑跳转(路由跳转) 四.JS原型 五.vue组件生命周期钩子 六.vue的ajax插件:axios 七.vu