zoukankan      html  css  js  c++  java
  • [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 service or data from parent to child component you can use @Provide and @Inject.

    Parent component:

    <template>
      <div class="hello">
        <ChildComp :msg="'What a good day!'"/>
      </div>
    </template>
    
    <script lang="ts">
    import Vue from 'vue'
    import {Component, Provide} from 'vue-property-decorator'
    
    import ChildComp from './Child.vue';
    
    @Component({
    })
    export default class Hello extends Vue {
    
      @Provide('users')
      users = [
        {
          name: 'test',
          id: 0
        }
      ]
    
    }
    </script>

    Child:

    <template>
      <div>
          {{users}}
      </div>
    </template>
    
    <script lang="ts">
    
    import Vue from 'vue'
    import {Component, Inject} from 'vue-property-decorator'
    
    @Component({})
    export default class Child extends Vue {
        message: string = "Hello";
    
        @Inject('users') users;
    }
    </script>
  • 相关阅读:
    js 获取时间差
    linq 两个list合并处理,并分组
    单例模式 双锁
    2018年的读书清单
    感悟
    asp.net使用Microsoft.mshtml提取网页标题等解析网页
    //利用反射快速给Model实体赋值
    C# url接口调用
    多字段动态查询
    对图片的操作
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7522484.html
Copyright © 2011-2022 走看看