码迷,mamicode.com
首页 > 其他好文 > 详细

[Vue + TS] Use Dependency Injection in Vue Using @Inject and @Provide Decorators with TypeScript

时间:2017-09-14 21:51:36      阅读:370      评论:0      收藏:0      [点我收藏+]

标签:dem   class   data   win   user   lan   ons   pen   int   

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>

 

[Vue + TS] Use Dependency Injection in Vue Using @Inject and @Provide Decorators with TypeScript

标签:dem   class   data   win   user   lan   ons   pen   int   

原文地址:http://www.cnblogs.com/Answer1215/p/7522484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!