[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>
时间: 2024-12-24 06:27:30

[Vue + TS] Use Dependency Injection in Vue Using @Inject and @Provide Decorators with TypeScript的相关文章

[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>

[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>

DEPENDENCY INJECTION EXAMPLE USING SPRING

Dependency Injection The Spring Framework is literally built around the concept of Dependency Injection. In this post, we’ll take a look at a simple example of Dependency Injection using the Spring Framework. If you want a deeper dive on Dependency I

【转】Understanding Inversion of Control, Dependency Injection and Service Locator Print

原文:https://www.dotnettricks.com/learn/dependencyinjection/understanding-inversion-of-control-dependency-injection-and-service-locator ----------------------------------------------------------------------------------------- Understanding Inversion of

[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搭建项目

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搭建工程

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