zoukankan      html  css  js  c++  java
  • [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 }
    })
  • 相关阅读:
    关于重载和重写的区别
    UML的关联(Association), 聚合(Aggregation), 组合(Composition)区别
    解析CSS加密技术之“障眼法”
    ASP.NET中MEMCACHED
    新建项目的无法应用已有项目
    C# 判断两张图片是否一致的快速方法
    压力测试中需要掌握的几个基本概念
    软件测试Web数据分析工具HttpWatch安装
    在SQL Server实现最短路径的搜索
    从算法入手讲解如何在SQL Server中实现最优最简
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7507184.html
Copyright © 2011-2022 走看看