zoukankan      html  css  js  c++  java
  • [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 how we can set types and even default properties on @Prop!

     

    Install:

    npm install --save vue-property-decorator

    Child:

    <template>
      <div>
          {{fullMessage}}
      </div>
    </template>
    
    <script lang="ts">
    
    import Vue from 'vue'
    import {Component, Prop} from 'vue-property-decorator'
    
    @Component({})
    export default class Child extends Vue {
        message: string = "Hello";
        @Prop({
            type: String,
            default: 'Default Value'
        }) msg: string;
    
        get fullMessage() {
            return `${this.message},${this.msg}`;
        }
    }
    </script>

    Parent:

    <template>
      <div class="hello">
        <h1 v-colorDirective="{color: 'red', backgroundColor: 'blue'}">{{ message }}</h1>
        <button @click="clicked">Click</button>
        <ChildComp msg="'What a good day!'"/>
        <router-link to="/hello-ts">Hello Ts</router-link>
      </div>
    </template>
    
    <script lang="ts">
    import Vue from 'vue'
    import Component from 'vue-class-component'
    import colorDirective from '../color-directive';
    
    import ChildComp from './Child.vue';
    
    @Component({
      directives: {
        colorDirective
      },
      components: {
        ChildComp
      }
    })
    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');
      }
    }
    </script>
  • 相关阅读:
    P1273 有线电视网
    P2015 二叉苹果树
    POJ 3659 Cell Phone Network
    POJ 1463 Strategic game
    NC51178 没有上司的舞会
    NC15033 小G有一个大树
    13. SpringBoot 日志框架的默认配置 和 指定日志文件 以及 ProFile 功能
    12. SpringBoot 日志框架的关系 研究中间包的替换
    11. SpringBoot 日志框架 — 解决和思路
    41.el和template区别 & VUE实现分离写法
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7516304.html
Copyright © 2011-2022 走看看